@synotech/utils
Version:
a collection of utilities for internal use
20 lines (19 loc) • 523 B
text/typescript
/**
* This method converts a date object to a specified timezone
* @module dateToTimezone
* @param {date} date - a date object
* @param {string} timezone - a timezone string
* @return {date} {Date} a date object in the specified timezone
* @example
*
* dateToTimezone(new Date(), 'America/New_York')
*
*/
export const dateToTimezone = (date: any, timezone: string): Date => {
return new Date(
(typeof date === 'string' ? new Date(date) : date).toLocaleString(
'en-US',
{ timeZone: timezone }
)
);
};