Skip to content

Instantly share code, notes, and snippets.

@anoopt
Last active March 31, 2022 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anoopt/0a428881bd67cbde611e977f25d7039e to your computer and use it in GitHub Desktop.
Save anoopt/0a428881bd67cbde611e977f25d7039e to your computer and use it in GitHub Desktop.
Get difference between local time and other time zone based time using Temporal or Luxon
<!-- Example -->
//const time = showTimeUsingTemporal ?
//await this.getOfficeLocalTimeUsingTemporal(officeTimeZoneId) :
//await this.getOfficeLocalTimeUsingLuxon(officeTimeZoneId);
<!-- Output -->
<!-- officeTimeZoneId = 'Asia/Kolkata' -->
//πŸ•™ 14:32 (4h 30m ahead of you)
<!-- officeTimeZoneId = 'Europe/London' -->
//πŸ•™ 10:02 (Same time zone as you)
<!-- officeTimeZoneId = 'Europe/Lisbon' -->
//πŸ•™ 10:02 (Same time as you)
<!-- officeTimeZoneId = 'America/Los_Angeles' -->
//πŸ•™ 02:02 (8h behind you)
//* See - https://moment.github.io/luxon/#/?id=luxon
private async getOfficeLocalTimeUsingLuxon(officeTimeZoneId: string): Promise<string> {
const luxon = await import(
/* webpackChunkName: 'luxon' */
'luxon'
);
try {
const { DateTime } = luxon;
const officeTime = DateTime.local().setZone(officeTimeZoneId);
if (!officeTime.isValid) {
return "";
}
const officeTimeToDisplay: string = `πŸ•™ ${officeTime.toLocaleString(DateTime.TIME_SIMPLE)}`;
const localTime = DateTime.local();
if (localTime.zoneName === officeTimeZoneId) {
return `${officeTimeToDisplay} (Same time zone as you)`;
}
const localTimeOffset: number = localTime.offset;
const officeTimeOffset: number = officeTime.offset;
return this.formatTimeUsingOffset(officeTimeToDisplay, officeTimeOffset - localTimeOffset);
} catch (error) {
console.error(error);
return "";
}
}
//* See - https://www.npmjs.com/package/@js-temporal/polyfill
private async getOfficeLocalTimeUsingTemporal(officeTimeZoneId: string): Promise<string> {
const jstemporal = await import(
/* webpackChunkName: 'jstemporal' */
'@js-temporal/polyfill'
);
try {
const { Temporal } = jstemporal;
const now = Temporal.Now;
const officeTime = now.zonedDateTimeISO(officeTimeZoneId);
const officeTimeToDisplay: string = `πŸ•™ ${String(officeTime.hour).padStart(2, '0')}:${String(officeTime.minute).padStart(2, '0')}`;
const localTime = now.zonedDateTimeISO();
if (localTime.timeZone.toString() === officeTimeZoneId) {
return `${officeTimeToDisplay} (Same time zone as you)`;
}
const localTimeOffset = localTime.offsetNanoseconds / 1000000000 / 60;
const officeTimeOffset = officeTime.offsetNanoseconds / 1000000000 / 60;
return this.formatTimeUsingOffset(officeTimeToDisplay, officeTimeOffset - localTimeOffset);
} catch (error) {
console.error(error);
return "";
}
}
private formatTimeUsingOffset(time: string, offset: number): string {
if (offset === 0) {
return `${time} (Same time as you)`;
}
const offsetHours: number = Math.abs(offset / 60 ^ 0);
const offsetMinutes: number = Math.abs(offset % 60);
const offsetHoursString: string = offsetHours > 0 ? `${offsetHours}h` : '';
const offsetMinutesString: string = offsetMinutes > 0 ? `${offsetMinutes}m` : '';
let offsetSuffix: string = `${offsetHoursString} ${offsetMinutesString} ${offset > 0 ? 'ahead of' : 'behind'} you`;
return `${time} (${offsetSuffix})`;
}
<!-- Example -->
//const time = showTimeUsingTemporal ?
//await this.getOfficeLocalTimeUsingTemporal(officeTimeZoneId) :
//await this.getOfficeLocalTimeUsingLuxon(officeTimeZoneId);
<!-- Output -->
<!-- officeTimeZoneId = 'Asia/Kolkata' -->
//πŸ•™ 14:32 (4h 30m ahead of you)
<!-- officeTimeZoneId = 'Europe/London' -->
//πŸ•™ 10:02 (Same time zone as you)
<!-- officeTimeZoneId = 'Europe/Lisbon' -->
//πŸ•™ 10:02 (Same time as you)
<!-- officeTimeZoneId = 'America/Los_Angeles' -->
//πŸ•™ 02:02 (8h behind you)
<!-- Dependencies -->
//"@js-temporal/polyfill": "^0.4.0" (Will not be needed later)
//"luxon": "^2.3.1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment