简体   繁体   中英

date-fns/format always takes local timezone

From below code:

 const dateString = '1994-09-15T12:00:00-03:00'; const parsedDate = parseISO(dateString) const dateFormat = 'MM-dd-yyyy HH:mm:ss xxx' console.log(format(parsedDate, dateFormat, { }))

I expect: 09-15-1994 20:30:00 -03:00 But i get 09-15-1994 20:30:00 +05:30 as my local timezone is +05:30

What am i missing here?

The parseDate() creates a date based on GMT, eg is adjusted by 3h based on your input '1994-09-15T12:00:00-03:00' .

The format() function with xxx formats local time without the Z , such as -08:00 , +05:30 , +00:00 . If you want to format with GMT string, specify OOOO , which will give you something like GMT-08:00 , GMT+05:30 , or GMT+00:00 , which again has the offset adjusted based on your browser's time zone.

See Time-Zones docs below if you want to format to a time zone other than your browser's one, such as GMT-03:00.

Docs:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM