Skip to content

Commit

Permalink
Customizing the isToday & isTomorrow & isYesterday functions to consi…
Browse files Browse the repository at this point in the history
…der timezones
  • Loading branch information
abzokhattab committed Sep 20, 2023
1 parent 8000576 commit fd9f36e
Showing 1 changed file with 46 additions and 6 deletions.
52 changes: 46 additions & 6 deletions src/libs/DateUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import {es, enGB} from 'date-fns/locale';
import {
formatDistanceToNow,
subMinutes,
addDays,
subDays,
isBefore,
subMilliseconds,
isToday,
isTomorrow,
isYesterday,
startOfWeek,
endOfWeek,
format,
Expand Down Expand Up @@ -85,6 +84,47 @@ function getLocalDateFromDatetime(locale, datetime, currentSelectedTimezone = ti
return utcToZonedTime(parsedDatetime, currentSelectedTimezone);
}

/**
* Checks if a given date is today in the specified time zone.
*
* @param {Date} date - The date to compare.
* @param {String} timeZone - The time zone to consider.
* @returns {Boolean} True if the date is today; otherwise, false.
*/
function isToday(date, timeZone) {
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} date - The date to compare.
* @param {String} timeZone - The time zone to consider.
* @returns {Boolean} True if the date is tomorrow; otherwise, false.
*/
function isTomorrow(date, timeZone) {
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} date - The date to compare.
* @param {String} timeZone - The time zone to consider.
* @returns {Boolean} True if the date is yesterday; otherwise, false.
*/
function isYesterday(date, timeZone) {
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
*
Expand Down Expand Up @@ -117,13 +157,13 @@ function datetimeToCalendarTime(locale, datetime, includeTimeZone = false, curre
yesterdayAt = yesterdayAt.toLowerCase();
}

if (isToday(date)) {
if (isToday(date, currentSelectedTimezone)) {
return `${todayAt} ${format(date, CONST.DATE.LOCAL_TIME_FORMAT)}${tz}`;
}
if (isTomorrow(date)) {
if (isTomorrow(date, currentSelectedTimezone)) {
return `${tomorrowAt} ${format(date, CONST.DATE.LOCAL_TIME_FORMAT)}${tz}`;
}
if (isYesterday(date)) {
if (isYesterday(date, currentSelectedTimezone)) {
return `${yesterdayAt} ${format(date, CONST.DATE.LOCAL_TIME_FORMAT)}${tz}`;
}
if (date >= startOfCurrentWeek && date <= endOfCurrentWeek) {
Expand Down

0 comments on commit fd9f36e

Please sign in to comment.