-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
171 lines (154 loc) · 5.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const dayjs = require("dayjs")
const YearController = require('./years')
const MonthController = require('./months')
const WeekController = require('./weeks')
const DaysController = require('./days')
const HoursController = require("./hours")
const MinutesController = require("./minutes")
const SecondsController = require("./seconds")
function helper(fromDate, toDate, unit, includeSeconds=true) {
fromDate = dayjs(fromDate)
toDate = dayjs(toDate)
let diff = toDate.diff(fromDate)
let totalSeconds = Math.floor(diff / 1000)
let totalMinutes = Math.floor(totalSeconds / 60)
let totalHours = Math.floor(totalMinutes / 60)
let totalDays = Math.floor(totalHours / 24)
const years = Math.floor(totalDays / 365)
totalDays = totalDays % 365
let months
if (totalDays < 360) {
months = Math.floor(totalDays / 30)
totalDays = totalDays % 30
} else {
months = Math.floor(totalDays / 30.417)
totalDays = totalDays % 30.417
}
const weeks = Math.floor(totalDays / 7)
totalDays = totalDays % 7
const days = Math.round(totalDays)
totalHours = totalHours % 24
const hours = totalHours
totalMinutes = totalMinutes % 60
const minutes = totalMinutes
totalSeconds = totalSeconds % 60
const seconds = totalSeconds
const returnObject = {
years: years,
months: months,
weeks: weeks,
days: days,
hours: hours,
minutes: minutes,
seconds: seconds,
}
if (years) {
const yearController = new YearController()
if (years === 1) {
const output = yearController.firstYear(years, months, weeks, days, hours, minutes, seconds)
return {...returnObject, ...output}
} else if (years > 10 && years < 100) {
const output = yearController.decades(years)
return {...returnObject, ...output}
} else if (years >= 100) {
const output = yearController.centuries(years)
return {...returnObject, ...output}
} else if (years > 1) {
const output = yearController.years(years, months, weeks, days, hours, minutes, seconds)
return {...returnObject, ...output}
}
} else if (months) {
const monthController = new MonthController()
if (months === 1) {
const output = monthController.firstMonth(years, months, weeks, days, hours, minutes, seconds)
return {...returnObject, ...output}
} else if (months > 1) {
const output = monthController.otherMonths(years, months, weeks, days, hours, minutes, seconds)
return {...returnObject, ...output}
}
} else if (weeks) {
const weekController = new WeekController()
if (weeks === 1) {
const output = weekController.firstWeek(weeks, days, hours, minutes, seconds)
return {...returnObject, ...output}
} else if (weeks > 1) {
const output = weekController.otherWeeks(months, weeks, days, hours, minutes, seconds)
return {...returnObject, ...output}
}
} else if (days) {
const dayController = new DaysController()
if (days === 1) {
const output = dayController.firstDay(days, hours, minutes, seconds)
return {...returnObject, ...output}
} else if (days > 1) {
const output = dayController.otherDays(weeks, days, hours, minutes, seconds)
return {...returnObject, ...output}
}
} else if (hours) {
const hoursController = new HoursController()
if (hours === 1) {
const output = hoursController.firstHour(hours, minutes, seconds)
return {...returnObject, ...output}
} else if (hours > 1) {
const output = hoursController.otherHours(days, hours, minutes, seconds)
return {...returnObject, ...output}
}
} else if (minutes) {
const minuteController = new MinutesController()
const output = minuteController.minutes(hours, minutes, seconds)
return {...returnObject, ...output}
} else if (seconds) {
const secondsController = new SecondsController()
const output = secondsController.seconds(seconds)
return {...returnObject, ...output}
} else {
return {
years: years,
months: months,
weeks: weeks,
days: days,
hours: hours,
minutes: minutes,
seconds: seconds,
value: `${seconds} seconds`,
prefix: "",
distance: `${seconds} seconds`
}
}
}
function distanceOfTimeInWords(from, to) {
const fromDate = dayjs(from)
const toDate = dayjs(to)
const diff = toDate.diff(fromDate)
if(Object.is(diff, NaN)) {
return "Invalid Date"
}
if (diff < 0) {
const result = helper(toDate,fromDate)
if (result) {
result.suffix = "ago"
result.distance += " ago"
return result
}
} else if (diff > 0) {
const result = helper(fromDate, toDate)
if (result) {
result.suffix = "later"
return result
}
} else {
return {
years: 0,
months: 0,
weeks: 0,
days: 0,
hours: 0,
minutes: 0,
seconds: 0,
value: "moments ago",
prefix: "",
distance: "moments ago"
}
}
}
module.exports = distanceOfTimeInWords