Created
June 22, 2023 15:32
-
-
Save linktohack/7a063ca7a39bc8da203b39a9bf236bd4 to your computer and use it in GitHub Desktop.
Understand date-fns-tz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const utcDate = new Date("2022-06-15T05:00:00Z"); | |
console.log("utcDate", utcDate, utcDate.toString()); | |
const utcDate2 = set(utcDate, { hours: 1 }); // Always set in curren time | |
console.log("utcDate2", utcDate2, utcDate2.toString()); | |
const zonedDate = utcToZonedTime(utcDate, "Asia/Jakarta"); // 5 + 7 = 12 in current timezone (pretend to be in local) | |
console.log("zoneDate", zonedDate, zonedDate.toString()); | |
const zonedDate2 = set(zonedDate, { hours: 1 }); | |
console.log("zoneDate2", zonedDate2, zonedDate2.toString()); // 1 in current timezone (pretend to be in local) | |
const backtoUtc = zonedTimeToUtc(zonedDate2, "Asia/Jakarta"); | |
console.log("backtoUtc", backtoUtc, backtoUtc.toString()); // 25 - 7 = 18 in UTC or 20 in Europe/Paris | |
console.log( | |
formatInTimeZone(backtoUtc, "Asia/Jakarta", "yyyy-MM-dd HH:mm:ss zzzz") | |
); | |
console.log( | |
format(zonedDate2, "yyyy-MM-dd HH:mm:ss zzzz", { | |
timeZone: "Asia/Jakarta", | |
}) | |
); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log | |
utcDate 2022-06-15T05:00:00.000Z Wed Jun 15 2022 07:00:00 GMT+0200 (Central European Summer Time) | |
at Object.<anonymous> (time.test.ts:118:13) | |
console.log | |
utcDate2 2022-06-14T23:00:00.000Z Wed Jun 15 2022 01:00:00 GMT+0200 (Central European Summer Time) | |
at Object.<anonymous> (time.test.ts:121:13) | |
console.log | |
zoneDate 2022-06-15T10:00:00.000Z Wed Jun 15 2022 12:00:00 GMT+0200 (Central European Summer Time) | |
at Object.<anonymous> (time.test.ts:124:13) | |
console.log | |
zoneDate2 2022-06-14T23:00:00.000Z Wed Jun 15 2022 01:00:00 GMT+0200 (Central European Summer Time) | |
at Object.<anonymous> (time.test.ts:127:13) | |
console.log | |
backtoUtc 2022-06-14T18:00:00.000Z Tue Jun 14 2022 20:00:00 GMT+0200 (Central European Summer Time) | |
at Object.<anonymous> (time.test.ts:130:13) | |
console.log | |
2022-06-15 01:00:00 Western Indonesia Time | |
at Object.<anonymous> (time.test.ts:132:13) | |
console.log | |
2022-06-15 01:00:00 Western Indonesia Time | |
at Object.<anonymous> (time.test.ts:135:13) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment