-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
DateUtils.ts
713 lines (638 loc) · 21.9 KB
/
DateUtils.ts
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
import {
addDays,
addHours,
addMinutes,
eachDayOfInterval,
eachMonthOfInterval,
endOfDay,
endOfWeek,
format,
formatDistanceToNow,
getDayOfYear,
isAfter,
isBefore,
isSameDay,
isSameSecond,
isSameYear,
isValid,
parse,
set,
setDefaultOptions,
startOfWeek,
subDays,
subMilliseconds,
subMinutes,
} from 'date-fns';
import {formatInTimeZone, format as tzFormat, utcToZonedTime, zonedTimeToUtc} from 'date-fns-tz';
import {enGB, es} from 'date-fns/locale';
import throttle from 'lodash/throttle';
import Onyx from 'react-native-onyx';
import {ValueOf} from 'type-fest';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import {SelectedTimezone, Timezone} from '@src/types/onyx/PersonalDetails';
import * as CurrentDate from './actions/CurrentDate';
import * as Localize from './Localize';
type CustomStatusTypes = (typeof CONST.CUSTOM_STATUS_TYPES)[keyof typeof CONST.CUSTOM_STATUS_TYPES];
type TimePeriod = 'AM' | 'PM';
type Locale = ValueOf<typeof CONST.LOCALES>;
let currentUserAccountID: number | undefined;
Onyx.connect({
key: ONYXKEYS.SESSION,
callback: (val) => {
// When signed out, val is undefined
if (!val) {
return;
}
currentUserAccountID = val.accountID;
},
});
let timezone: Required<Timezone> = CONST.DEFAULT_TIME_ZONE;
Onyx.connect({
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (value) => {
if (!currentUserAccountID) {
return;
}
const personalDetailsTimezone = value?.[currentUserAccountID]?.timezone;
timezone = {
selected: personalDetailsTimezone?.selected ?? CONST.DEFAULT_TIME_ZONE.selected,
automatic: personalDetailsTimezone?.automatic ?? CONST.DEFAULT_TIME_ZONE.automatic,
};
},
});
/**
* Gets the locale string and setting default locale for date-fns
*/
function setLocale(localeString: Locale) {
switch (localeString) {
case CONST.LOCALES.EN:
setDefaultOptions({locale: enGB});
break;
case CONST.LOCALES.ES:
setDefaultOptions({locale: es});
break;
default:
break;
}
}
/**
* Gets the user's stored time zone NVP and returns a localized
* Date object for the given ISO-formatted datetime string
*/
function getLocalDateFromDatetime(locale: Locale, datetime?: string, currentSelectedTimezone: SelectedTimezone = timezone.selected): Date {
setLocale(locale);
if (!datetime) {
return utcToZonedTime(new Date(), currentSelectedTimezone);
}
const parsedDatetime = new Date(`${datetime}Z`);
return utcToZonedTime(parsedDatetime, currentSelectedTimezone);
}
/**
* Checks if a given date is today in the specified time zone.
*
* @param date - The date to compare.
* @param timeZone - The time zone to consider.
* @returns True if the date is today; otherwise, false.
*/
function isToday(date: Date, timeZone: SelectedTimezone): boolean {
const currentDate = new Date();
const currentDateInTimeZone = utcToZonedTime(currentDate, timeZone);
return isSameDay(date, currentDateInTimeZone);
}
/**
* Checks if a given date is tomorrow in the specified time zone.
*
* @param date - The date to compare.
* @param timeZone - The time zone to consider.
* @returns True if the date is tomorrow; otherwise, false.
*/
function isTomorrow(date: Date, timeZone: SelectedTimezone): boolean {
const currentDate = new Date();
const tomorrow = addDays(currentDate, 1); // Get the date for tomorrow in the current time zone
const tomorrowInTimeZone = utcToZonedTime(tomorrow, timeZone);
return isSameDay(date, tomorrowInTimeZone);
}
/**
* Checks if a given date is yesterday in the specified time zone.
*
* @param date - The date to compare.
* @param timeZone - The time zone to consider.
* @returns True if the date is yesterday; otherwise, false.
*/
function isYesterday(date: Date, timeZone: SelectedTimezone): boolean {
const currentDate = new Date();
const yesterday = subDays(currentDate, 1); // Get the date for yesterday in the current time zone
const yesterdayInTimeZone = utcToZonedTime(yesterday, timeZone);
return isSameDay(date, yesterdayInTimeZone);
}
/**
* Formats an ISO-formatted datetime string to local date and time string
*
* e.g.
*
* Jan 20 at 5:30 PM within the past year
* Jan 20, 2019 at 5:30 PM anything over 1 year ago
*/
function datetimeToCalendarTime(locale: Locale, datetime: string, includeTimeZone = false, currentSelectedTimezone: SelectedTimezone = timezone.selected, isLowercase = false): string {
const date = getLocalDateFromDatetime(locale, datetime, currentSelectedTimezone);
const tz = includeTimeZone ? ' [UTC]Z' : '';
let todayAt = Localize.translate(locale, 'common.todayAt');
let tomorrowAt = Localize.translate(locale, 'common.tomorrowAt');
let yesterdayAt = Localize.translate(locale, 'common.yesterdayAt');
const at = Localize.translate(locale, 'common.conjunctionAt');
const startOfCurrentWeek = startOfWeek(new Date(), {weekStartsOn: 1}); // Assuming Monday is the start of the week
const endOfCurrentWeek = endOfWeek(new Date(), {weekStartsOn: 1}); // Assuming Monday is the start of the week
if (isLowercase) {
todayAt = todayAt.toLowerCase();
tomorrowAt = tomorrowAt.toLowerCase();
yesterdayAt = yesterdayAt.toLowerCase();
}
if (isToday(date, currentSelectedTimezone)) {
return `${todayAt} ${format(date, CONST.DATE.LOCAL_TIME_FORMAT)}${tz}`;
}
if (isTomorrow(date, currentSelectedTimezone)) {
return `${tomorrowAt} ${format(date, CONST.DATE.LOCAL_TIME_FORMAT)}${tz}`;
}
if (isYesterday(date, currentSelectedTimezone)) {
return `${yesterdayAt} ${format(date, CONST.DATE.LOCAL_TIME_FORMAT)}${tz}`;
}
if (date >= startOfCurrentWeek && date <= endOfCurrentWeek) {
return `${format(date, CONST.DATE.MONTH_DAY_ABBR_FORMAT)} ${at} ${format(date, CONST.DATE.LOCAL_TIME_FORMAT)}${tz}`;
}
return `${format(date, CONST.DATE.MONTH_DAY_YEAR_ABBR_FORMAT)} ${at} ${format(date, CONST.DATE.LOCAL_TIME_FORMAT)}${tz}`;
}
/**
* Converts an ISO-formatted datetime string into a localized string representation
* that's relative to current moment in time.
*
* e.g.
*
* < 1 minute ago within the past minute
* 12 minutes ago within the past hour
* 1 hour ago within the past day
* 3 days ago within the past month
* Jan 20 within the past year
* Jan 20, 2019 anything over 1 year
*/
function datetimeToRelative(locale: Locale, datetime: string): string {
const date = getLocalDateFromDatetime(locale, datetime);
return formatDistanceToNow(date, {addSuffix: true});
}
/**
* Gets the zone abbreviation from the date
*
* e.g.
*
* PST
* EST
* GMT +07 - For GMT timezone
*
* @param datetime
* @param selectedTimezone
* @returns
*/
function getZoneAbbreviation(datetime: string | Date, selectedTimezone: SelectedTimezone): string {
return formatInTimeZone(datetime, selectedTimezone, 'zzz');
}
/**
* Format date to a long date format with weekday
*
* @returns Sunday, July 9, 2023
*/
function formatToLongDateWithWeekday(datetime: string): string {
return format(new Date(datetime), CONST.DATE.LONG_DATE_FORMAT_WITH_WEEKDAY);
}
/**
* Format date to a weekday format
*
* @returns Sunday
*/
function formatToDayOfWeek(datetime: string): string {
return format(new Date(datetime), CONST.DATE.WEEKDAY_TIME_FORMAT);
}
/**
* Format date to a local time
*
* @returns 2:30 PM
*/
function formatToLocalTime(datetime: string | Date): string {
return format(new Date(datetime), CONST.DATE.LOCAL_TIME_FORMAT);
}
const THREE_HOURS = 1000 * 60 * 60 * 3;
/**
* A throttled version of a function that updates the current date in Onyx store
*/
const updateCurrentDate = throttle(() => {
const currentDate = format(new Date(), CONST.DATE.FNS_FORMAT_STRING);
CurrentDate.setCurrentDate(currentDate);
}, THREE_HOURS);
/**
* Initialises the event listeners that trigger the current date update
*/
function startCurrentDateUpdater() {
const trackedEvents = ['mousemove', 'touchstart', 'keydown', 'scroll'];
trackedEvents.forEach((eventName) => {
document.addEventListener(eventName, updateCurrentDate);
});
}
function getCurrentTimezone(): Required<Timezone> {
const currentTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
if (timezone.automatic && timezone.selected !== currentTimezone) {
return {...timezone, selected: currentTimezone as SelectedTimezone};
}
return timezone;
}
/**
* @returns [January, Fabruary, March, April, May, June, July, August, ...]
*/
function getMonthNames(preferredLocale: Locale): string[] {
if (preferredLocale) {
setLocale(preferredLocale);
}
const fullYear = new Date().getFullYear();
const monthsArray = eachMonthOfInterval({
start: new Date(fullYear, 0, 1), // January 1st of the current year
end: new Date(fullYear, 11, 31), // December 31st of the current year
});
// eslint-disable-next-line rulesdir/prefer-underscore-method
return monthsArray.map((monthDate) => format(monthDate, CONST.DATE.MONTH_FORMAT));
}
/**
* @returns [Monday, Thuesday, Wednesday, ...]
*/
function getDaysOfWeek(preferredLocale: Locale): string[] {
if (preferredLocale) {
setLocale(preferredLocale);
}
const startOfCurrentWeek = startOfWeek(new Date(), {weekStartsOn: 1}); // Assuming Monday is the start of the week
const endOfCurrentWeek = endOfWeek(new Date(), {weekStartsOn: 1}); // Assuming Monday is the start of the week
const daysOfWeek = eachDayOfInterval({start: startOfCurrentWeek, end: endOfCurrentWeek});
// eslint-disable-next-line rulesdir/prefer-underscore-method
return daysOfWeek.map((date) => format(date, 'eeee'));
}
// Used to throttle updates to the timezone when necessary
let lastUpdatedTimezoneTime = new Date();
function canUpdateTimezone(): boolean {
const currentTime = new Date();
const fiveMinutesAgo = subMinutes(currentTime, 5);
// Compare the last updated time with five minutes ago
return isBefore(lastUpdatedTimezoneTime, fiveMinutesAgo);
}
function setTimezoneUpdated() {
lastUpdatedTimezoneTime = new Date();
}
/**
* Get the UNIX timestamp in microseconds, with millisecond precision.
*/
function getMicroseconds(): number {
return Date.now() * CONST.MICROSECONDS_PER_MS;
}
/**
* Returns the current time in milliseconds in the format expected by the database
*/
function getDBTime(timestamp: string | number = ''): string {
const datetime = timestamp ? new Date(timestamp) : new Date();
return datetime.toISOString().replace('T', ' ').replace('Z', '');
}
function subtractMillisecondsFromDateTime(dateTime: string, milliseconds: number): string {
const date = zonedTimeToUtc(dateTime, 'UTC');
const newTimestamp = subMilliseconds(date, milliseconds).valueOf();
return getDBTime(newTimestamp);
}
/**
* @param isoTimestamp example: 2023-05-16 05:34:14.388
* @returns example: 2023-05-16
*/
function getDateStringFromISOTimestamp(isoTimestamp: string): string {
if (!isoTimestamp) {
return '';
}
const [dateString] = isoTimestamp.split(' ');
return dateString;
}
/**
* returns {string} example: 2023-05-16 05:34:14
*/
function getThirtyMinutesFromNow(): string {
const date = addMinutes(new Date(), 30);
return format(date, 'yyyy-MM-dd HH:mm:ss');
}
/**
* returns {string} example: 2023-05-16 05:34:14
*/
function getOneHourFromNow(): string {
const date = addHours(new Date(), 1);
return format(date, 'yyyy-MM-dd HH:mm:ss');
}
/**
* returns {string} example: 2023-05-16 05:34:14
*/
function getEndOfToday(): string {
const date = endOfDay(new Date());
return format(date, 'yyyy-MM-dd HH:mm:ss');
}
/**
* returns {string} example: 2023-05-16 05:34:14
*/
function getOneWeekFromNow(): string {
const date = addDays(new Date(), 7);
return format(date, 'yyyy-MM-dd HH:mm:ss');
}
/**
* param {string} dateTimeString
* returns {string} example: 2023-05-16
*/
function extractDate(dateTimeString: string): string {
if (!dateTimeString) {
return '';
}
if (dateTimeString === 'never') {
return '';
}
const date = new Date(dateTimeString);
return format(date, 'yyyy-MM-dd');
}
/**
* param {string} dateTimeString
* returns {string} example: 11:10 PM
*/
function extractTime12Hour(dateTimeString: string): string {
if (!dateTimeString || dateTimeString === 'never') {
return '';
}
const date = new Date(dateTimeString);
return format(date, 'hh:mm a');
}
/**
* param {string} dateTimeString
* returns {string} example: 2023-05-16 11:10 PM
*/
function formatDateTimeTo12Hour(dateTimeString: string): string {
if (!dateTimeString) {
return '';
}
const date = new Date(dateTimeString);
return format(date, 'yyyy-MM-dd hh:mm a');
}
/**
* param {string} type - one of the values from CONST.CUSTOM_STATUS_TYPES
* returns {string} example: 2023-05-16 11:10:00 or ''
*/
function getDateFromStatusType(type: CustomStatusTypes): string {
switch (type) {
case CONST.CUSTOM_STATUS_TYPES.THIRTY_MINUTES:
return getThirtyMinutesFromNow();
case CONST.CUSTOM_STATUS_TYPES.ONE_HOUR:
return getOneHourFromNow();
case CONST.CUSTOM_STATUS_TYPES.AFTER_TODAY:
return getEndOfToday();
case CONST.CUSTOM_STATUS_TYPES.AFTER_WEEK:
return getOneWeekFromNow();
case CONST.CUSTOM_STATUS_TYPES.NEVER:
return CONST.CUSTOM_STATUS_TYPES.NEVER;
default:
return '';
}
}
/**
* param {string} data - either a value from CONST.CUSTOM_STATUS_TYPES or a dateTime string in the format YYYY-MM-DD HH:mm
* returns {string} example: 2023-05-16 11:10 PM or 'Today'
*/
function getLocalizedTimePeriodDescription(data: string): string {
const {translateLocal} = Localize;
switch (data) {
case getEndOfToday():
return translateLocal('statusPage.timePeriods.afterToday');
case CONST.CUSTOM_STATUS_TYPES.NEVER:
case '':
return translateLocal('statusPage.timePeriods.never');
default:
return formatDateTimeTo12Hour(data);
}
}
/**
* receive date like 2020-05-16 05:34:14 and format it to show in string like "Until 05:34 PM"
*/
function getStatusUntilDate(inputDate: string): string {
if (!inputDate) {
return '';
}
const {translateLocal} = Localize;
const input = new Date(inputDate);
const now = new Date();
const endOfToday = endOfDay(now);
// If the date is equal to the end of today
if (isSameDay(input, endOfToday)) {
return translateLocal('statusPage.untilTomorrow');
}
// If it's a time on the same date
if (isSameDay(input, now)) {
return translateLocal('statusPage.untilTime', {time: format(input, CONST.DATE.LOCAL_TIME_FORMAT)});
}
// If it's further in the future than tomorrow but within the same year
if (isAfter(input, now) && isSameYear(input, now)) {
return translateLocal('statusPage.untilTime', {time: format(input, `${CONST.DATE.SHORT_DATE_FORMAT} ${CONST.DATE.LOCAL_TIME_FORMAT}`)});
}
// If it's in another year
return translateLocal('statusPage.untilTime', {time: format(input, `${CONST.DATE.FNS_FORMAT_STRING} ${CONST.DATE.LOCAL_TIME_FORMAT}`)});
}
/**
* Update the time for a given date.
*
* param {string} updatedTime - Time in "hh:mm A" or "HH:mm:ss" or "yyyy-MM-dd HH:mm:ss" format.
* param {string} inputDateTime - Date in "YYYY-MM-DD HH:mm:ss" or "YYYY-MM-DD" format.
* returns {string} - Date with updated time in "YYYY-MM-DD HH:mm:ss" format.
*/
const combineDateAndTime = (updatedTime: string, inputDateTime: string): string => {
if (!updatedTime || !inputDateTime) {
return '';
}
let parsedTime: Date | null = null;
if (updatedTime.includes('-')) {
// it's in "yyyy-MM-dd HH:mm:ss" format
const tempTime = parse(updatedTime, 'yyyy-MM-dd HH:mm:ss', new Date());
if (isValid(tempTime)) {
parsedTime = tempTime;
}
} else if (updatedTime.includes(':')) {
// it's in "hh:mm a" format
const tempTime = parse(updatedTime, 'hh:mm a', new Date());
if (isValid(tempTime)) {
parsedTime = tempTime;
}
}
if (!parsedTime) {
return '';
}
let parsedDateTime: Date | null = null;
if (inputDateTime.includes(':')) {
// Check if it includes time
const tempDateTime = parse(inputDateTime, 'yyyy-MM-dd HH:mm:ss', new Date());
if (isValid(tempDateTime)) {
parsedDateTime = tempDateTime;
}
} else {
const tempDateTime = parse(inputDateTime, 'yyyy-MM-dd', new Date());
if (isValid(tempDateTime)) {
parsedDateTime = tempDateTime;
}
}
if (!parsedDateTime) {
return '';
}
const updatedDateTime = set(parsedDateTime, {
hours: parsedTime.getHours(),
minutes: parsedTime.getMinutes(),
seconds: parsedTime.getSeconds(),
});
return format(updatedDateTime, 'yyyy-MM-dd HH:mm:ss');
};
/**
* param {String} dateTime in 'HH:mm:ss' format
* returns {Object}
* example {hour: '11', minute: '10', period: 'AM'}
*/
function get12HourTimeObjectFromDate(dateTime: string): {hour: string; minute: string; period: string} {
if (!dateTime) {
return {
hour: '12',
minute: '00',
period: 'PM',
};
}
const parsedTime = parse(dateTime, 'hh:mm a', new Date());
return {
hour: format(parsedTime, 'hh'),
minute: format(parsedTime, 'mm'),
period: format(parsedTime, 'a').toUpperCase(),
};
}
/**
* param {String} timeString
* returns {String}
* example getTimePeriod('11:10 PM') // 'PM'
*/
function getTimePeriod(timeString: string): TimePeriod {
const parts = timeString.split(' ');
return parts[1] as TimePeriod;
}
/**
* param {String} dateTimeStringFirst // YYYY-MM-DD HH:mm:ss
* param {String} dateTimeStringSecond // YYYY-MM-DD HH:mm:ss
* returns {Boolean}
*/
function areDatesIdentical(dateTimeStringFirst: string, dateTimeStringSecond: string): boolean {
const date1 = parse(dateTimeStringFirst, 'yyyy-MM-dd HH:mm:ss', new Date());
const date2 = parse(dateTimeStringSecond, 'yyyy-MM-dd HH:mm:ss', new Date());
return isSameSecond(date1, date2);
}
/**
* Checks if the time input is at least one minute in the future.
* param {String} timeString: '04:24 AM'
* param {String} dateTimeString: '2023-11-14 14:24:00'
* returns {Boolean}
*/
const isTimeAtLeastOneMinuteInFuture = ({timeString, dateTimeString}: {timeString?: string; dateTimeString: string}): boolean => {
let dateToCheck = dateTimeString;
if (timeString) {
// return false;
// Parse the hour and minute from the time input
const [hourStr] = timeString.split(/[:\s]+/);
const hour = parseInt(hourStr, 10);
if (hour === 0) {
return false;
}
dateToCheck = combineDateAndTime(timeString, dateTimeString);
}
// Get current date and time
const now = new Date();
// Check if the combinedDate is at least one minute later than the current date and time
return isAfter(new Date(dateToCheck), addMinutes(now, 1));
};
/**
* Checks if the input date is in the future compared to the reference date.
* param {Date} inputDate - The date to validate.
* param {Date} referenceDate - The date to compare against.
* returns {string} - Returns an error key if validation fails, otherwise an empty string.
*/
const getDayValidationErrorKey = (inputDate: Date): string => {
if (!inputDate) {
return '';
}
const currentYear = getDayOfYear(new Date());
const inputYear = getDayOfYear(inputDate);
if (inputYear < currentYear) {
return 'common.error.invalidDateShouldBeFuture';
}
return '';
};
/**
* Checks if the input time is at least one minute in the future compared to the reference time.
* param {Date} inputTime - The time to validate.
* param {Date} referenceTime - The time to compare against.
* returns {string} - Returns an error key if validation fails, otherwise an empty string.
*/
const getTimeValidationErrorKey = (inputTime: Date): string => {
const timeNowPlusOneMinute = addMinutes(new Date(), 1);
if (isBefore(inputTime, timeNowPlusOneMinute)) {
return 'common.error.invalidTimeShouldBeFuture';
}
return '';
};
/**
*
* Get a date and format this date using the UTC timezone.
* param datetime
* param dateFormat
* returns If the date is valid, returns the formatted date with the UTC timezone, otherwise returns an empty string.
*/
function formatWithUTCTimeZone(datetime: string, dateFormat: string = CONST.DATE.FNS_FORMAT_STRING) {
const date = new Date(datetime);
if (isValid(date)) {
return tzFormat(utcToZonedTime(date, 'UTC'), dateFormat);
}
return '';
}
const DateUtils = {
formatToDayOfWeek,
formatToLongDateWithWeekday,
formatToLocalTime,
getZoneAbbreviation,
datetimeToRelative,
datetimeToCalendarTime,
startCurrentDateUpdater,
getLocalDateFromDatetime,
getCurrentTimezone,
canUpdateTimezone,
setTimezoneUpdated,
getMicroseconds,
getDBTime,
setLocale,
subtractMillisecondsFromDateTime,
getDateStringFromISOTimestamp,
getThirtyMinutesFromNow,
getEndOfToday,
getOneWeekFromNow,
getDateFromStatusType,
getOneHourFromNow,
extractDate,
formatDateTimeTo12Hour,
getStatusUntilDate,
extractTime12Hour,
get12HourTimeObjectFromDate,
areDatesIdentical,
getTimePeriod,
getLocalizedTimePeriodDescription,
combineDateAndTime,
getDayValidationErrorKey,
getTimeValidationErrorKey,
isToday,
isTomorrow,
isYesterday,
getMonthNames,
getDaysOfWeek,
formatWithUTCTimeZone,
isTimeAtLeastOneMinuteInFuture,
};
export default DateUtils;