Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
112
rated 0 times [  116] [ 4]  / answers: 1 / hits: 114451  / 5 Years ago, fri, october 25, 2019, 12:00:00

Problem



It looks like when I use the format() function, it automatically convert the original UTC time into my timezone (UTC+8). I have been digging through their docs for hours and couldn't seem to find a way to default it to UTC time.



import { parseISO, format } from date-fns;

const time = 2019-10-25T08:10:00Z;

const parsedTime = parseISO(time);
console.log(parsedTime); // 2019-10-25T08:10:00.000Z

const formattedTime = format(parsedTime, yyyy-MM-dd kk:mm:ss);
console.log(formattedTime); // 2019-10-25 16:10:00 <-- 8 HOURS OFF!!


I have tried to use the package data-fns-tz and use something like



format(parsedTime, yyyy-MM-dd kk:mm:ss, {timeZone: UTC});


still no luck.



Please help!



Expected Output



2019-10-25 08:10:00



Actual Output



2019-10-25 16:10:00


More From » date

 Answers
22

I would suggest using the built-in Date util:


const date = new Date("2019-10-25T08:10:00Z");
const isoDate = date.toISOString();

console.log(`${isoDate.substring(0, 10)} ${isoDate.substring(11, 19)}`);

Outputs:


2019-10-25 08:10:00


Not a general solution for any format, but no external libraries required.


[#51541] Wednesday, October 16, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
katelynn

Total Points: 378
Total Questions: 86
Total Answers: 108

Location: Azerbaijan
Member since Fri, May 12, 2023
1 Year ago
;