-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateTimeUtils.ts
More file actions
359 lines (295 loc) · 12.1 KB
/
DateTimeUtils.ts
File metadata and controls
359 lines (295 loc) · 12.1 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import moment, { Moment } from 'moment';
import { addDays, subDays } from "date-fns";
export const DATE_FORMAT = 'DD-MM-YYYY';
export const DATE_FORMAT_SECOND_FORMAT = 'DD-MMM-YYYY';
export const TIME_FORMAT = 'HH:mm';
export const DATE_TIME_FORMAT = 'dd-MM-yyyy HH:mm';
export const DATE_TIME_SECOND_FORMAT = 'DD-MM-YYYY hh:mm:ss';
export const DATE_TIME_FORMAT_A = 'YYYY-MM-DD hh:mm A';
export const DATE_YEAR_FORMAT = 'YYYY';
export const TIME_FORMAT_A = 'HH:mm A';
export default class DateTimeUtils {
public momentCurrentDateTimeObject = () => {
return moment();
}
public startOfDay = () => {
return moment().startOf('day');
}
public zeroPadding = (number: number) => {
return `${number}`.padStart(2, '0')
}
// currentDate
public currentDate = (dateFormat: string = DATE_FORMAT) => {
return this.formatDate(new Date(), dateFormat);
}
// currentTime
public currentTime = () => {
return this.formatTime(new Date());
}
// currentDateTime
public currentDateTime = (dateFormat: string = DATE_TIME_FORMAT) => {
return this.formatDateTime(new Date(), dateFormat);
}
// currentTimestamp
public currentTimestamp = () => {
return this.formatTimestamp(new Date());
}
// formatDate
public formatDate = (date: Moment | Date | string, dateFormat: string = DATE_FORMAT) => {
if (date === null || date === undefined) { return ''}
if (typeof date === 'object' && 'format' in date) {
return date.format(dateFormat);
}
return moment(date).format(dateFormat);
}
// formatTime
public formatTime = (time: Moment | Date | string, timeFormat: string = TIME_FORMAT) => {
if (time === null || time === undefined) { return ''}
if (typeof time === 'object' && 'format' in time) {
return time.format(timeFormat);
}
return moment(time).format(timeFormat);
}
public formatTimeA = (time: Moment | Date | string, timeFormat: string = TIME_FORMAT_A) => {
if (time === null || time === undefined) { return ''}
if (typeof time === 'object' && 'format' in time) {
return time.format(timeFormat);
}
return moment(time).format(timeFormat);
}
// formatDateTime
public formatDateTime= (time: Moment | Date | string, dateTimeFormat: string = DATE_TIME_FORMAT) => {
if (time === null || time === undefined) { return ''}
if (typeof time === 'object' && 'format' in time) {
return time.format(dateTimeFormat);
}
return moment(time).format(dateTimeFormat);
}
public formatDateTimeSecond= (time: Moment | Date | string, dateTimeFormat: string = DATE_TIME_SECOND_FORMAT) => {
if (time === null || time === undefined) { return ''}
if (typeof time === 'object' && 'format' in time) {
return time.format(dateTimeFormat);
}
return moment(time).format(dateTimeFormat);
}
public formatDateTimeA= (time: Moment | Date | string, dateTimeFormat: string = DATE_TIME_FORMAT_A) => {
if (time === null || time === undefined) { return ''}
if (typeof time === 'object' && 'format' in time) {
return time.format(dateTimeFormat);
}
return moment(time).format(dateTimeFormat);
}
public formatDateTimeB = (time: Moment | Date | string, dateTimeFormat: string = DATE_FORMAT_SECOND_FORMAT) => {
if (time === null || time === undefined) { return '' }
if (typeof time === 'object' && 'format' in time) {
return time.format(dateTimeFormat);
}
return moment(time).format(dateTimeFormat);
}
// formatTimestamp
public formatTimestamp = (date: Date | string) => {
if (date === null || date === undefined) { return ''}
if (date instanceof Date) {
return date.valueOf();
}
else {
const tempDate = new Date(date);
return tempDate.valueOf();
}
}
// Time Range
public formatTimeRange = (start: Date | string, end: Date | string) => {
return `${this.formatTime(start)} ~ ${this.formatTime(end)}`
}
public convertStringToDate = (date: Moment | Date | string | any) => {
if (date === '' || date === undefined || date === null) {
return null;
}
let stringToDate = moment(date);
return stringToDate['_d'] == 'Invalid Date' ? null : stringToDate;
}
public convertStringToYear = (date: Moment | Date | string) => {
//let stringToYear = moment(date).format('YYYY')
let stringToYear = moment().year(Number(date));
return stringToYear['_d'] == 'Invalid Year' ? null : stringToYear;
}
public convertStringToYearGrid = (date: Moment | Date | string) => {
let stringToYear = moment(date).format('YYYY')
// let stringToYear = moment().year(Number(date));
return stringToYear['_d'] == 'Invalid Year' ? null : stringToYear;
}
// compareDate
public compareDate = (curTime: Date, destTime: Date) => {
}
public addDays = (days: any, dateFormat: string = DATE_FORMAT) => {
if (days === null || days === undefined) { return ''}
const today = new Date();
const date = addDays(today, days);
return moment(date).format(dateFormat);
}
public subDays = (days: any, dateFormat: string = DATE_FORMAT) => {
if (days === null || days === undefined) { return ''}
const today = new Date();
const date = subDays(today, days);
return moment(date).format(dateFormat);
}
public timeDuration = (d: Date | string): any => {
const seconds = Math.floor(Math.abs(new Date().getTime() - new Date(d).getTime()) / 1000);
const year = Math.floor(seconds / 31536000);
const month = Math.floor(seconds / 2592000);
const days = Math.floor(seconds / 86400);
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor(seconds / 60);
return {
year: year,
month: month,
days: days,
hours: hours,
minutes: minutes,
seconds: seconds,
};
}
// timeAgo
public timeAgo = (date: Date | string) => {
if (date == null) { return ''}
const { days, hours, minutes, seconds } = this.timeDuration(date);
if (days > 3) {
return this.formatDate(date);
}
if (days > 0 && days <= 3) {
return `${days} days ago`;
}
if (days <= 0 && hours > 0) {
return `${hours} hours ago`;
}
if (hours <= 0 && minutes > 0) {
return `${minutes} mints ago`;
}
if (seconds < 60 && seconds > 0) {
return `${seconds} seconds ago`;
}
return '';
}
public daysAgo = (date: Date | string) => {
if (date == null) { return ''}
const { days, hours, minutes, seconds } = this.timeDuration(date);
if (days > 0) {
return `${days} days ago`;
}
if (days <= 0 && hours > 0) {
return `${hours} hours ago`;
}
if (hours <= 0 && minutes > 0) {
return `${minutes} mints ago`;
}
if (seconds < 60 && seconds > 0) {
return `${seconds} seconds ago`;
}
return '';
}
public diffDays = (startDate: Date | string, endDate: Date | string) => {
var now = moment(startDate);
var end = moment(endDate);
var duration = moment.duration(now.diff(end));
var days = duration.asDays();
return days
}
public ageCalculation = (birthDate: Date | string) => {
var now = new Date();
var today = new Date(now.getFullYear(),now.getMonth(),now.getDate());
var yearNow = now.getFullYear();
var monthNow = now.getMonth();
var dateNow = now.getDate();
var days=['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31'];
var months = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];
var ddd = months[new Date(birthDate).getMonth()] + "/" + days[new Date(birthDate).getDate() -1] + "/" + new Date(birthDate).getFullYear();
var firstsubString=Number(ddd.toString().substring(6,10));
var secondsubString=Number(ddd.toString().substring(0,2))-1;
var thirdsubString=Number(ddd.toString().substring(3,5));
var dob = new Date(firstsubString, secondsubString, thirdsubString);
var yearDob = dob.getFullYear();
var monthDob = dob.getMonth();
var dateDob = dob.getDate();
var age = {};
var ageString = "";
var yearString = "";
var monthString = "";
var dayString = "";
let yearAge = yearNow - yearDob;
if (monthNow >= monthDob)
var monthAge = monthNow - monthDob;
else {
yearAge--;
var monthAge = 12 + monthNow -monthDob;
}
if (dateNow >= dateDob)
var dateAge = dateNow - dateDob;
else {
monthAge--;
var dateAge = 31 + dateNow - dateDob;
if (monthAge < 0) {
monthAge = 11;
yearAge--;
}
}
age = {
years: yearAge,
months: monthAge,
days: dateAge
};
if (age['years'] > 1 ) yearString = " years";
else yearString = " year";
if ( age['months'] > 1 ) monthString = " months";
else monthString = " month";
if ( age['days'] > 1 ) dayString = " days";
else dayString = " day";
if ( (age['years'] > 0) && (age['months'] > 0) && (age['days'] > 0) )
ageString = age['years'] + yearString + ", " + age['months'] + monthString + ", and " + age['days'] + dayString + ".";
else if ( (age['years'] == 0) && (age['months'] == 0) && (age['days'] > 0) )
ageString = "Only " + age['days'] + dayString + ".";
else if ( (age['years'] > 0) && (age['months'] == 0) && (age['days'] == 0) )
ageString = age['years'] + yearString + " old. Happy Birthday!!";
else if ( (age['years'] > 0) && (age['months'] > 0) && (age['days'] == 0) )
ageString = age['years'] + yearString + " and " + age['months'] + monthString + ".";
else if ( (age['years'] == 0) && (age['months'] > 0) && (age['days'] > 0) )
ageString = age['months'] + monthString + " and " + age['days'] + dayString + ".";
else if ( (age['years'] > 0) && (age['months'] == 0) && (age['days'] > 0) )
ageString = age['years'] + yearString + " and " + age['days'] + dayString + ".";
else if ( (age['years'] == 0) && (age['months'] > 0) && (age['days'] == 0) )
ageString = age['months'] + monthString + ".";
else ageString = "Oops! Could not calculate age!";
return ageString;
// var age;
// var dob = new Date(birthDate);
// if (dob != undefined) {
// var todayDate = new Date();
// var ageyear = todayDate.getFullYear() - dob.getFullYear();
// var agemonth = todayDate.getMonth() - dob.getMonth();
// var ageday = (todayDate.getDate() - dob.getDate());
// if (agemonth < 0) {
// ageyear--;
// agemonth = 12 - 1;
// //agemonth = (12 + agemonth);
// }
// if (ageday < 0) {
// // ageyear--;
// agemonth = 12 - 1;
// ageday = 31 + ageday ;
// }
// if (agemonth == 12) {
// ageyear = ageyear + 1;
// agemonth = 0;
// }
// age = ageyear + ' Year, ' + agemonth + ' Month, ' + ageday + ' Day';
// return age;
// }
}
public getAgeYear = (birthDate: Date | string) => {
var dob = new Date(birthDate);
if (dob != undefined) {
var todayDate = new Date();
var ageyear = todayDate.getFullYear() - dob.getFullYear();
return ageyear;
}
}
}