简体   繁体   English

date-fns-timezone 给出错误的结果?

[英]date-fns-timezone Giving Wrong Result?

Right now it's Sunday Aug 30, 2020 5:20pm PST.现在是太平洋标准时间 2020 年 8 月 30 日星期日下午 5:20。

If I run this:如果我运行这个:

import {formatToTimeZone } from 'date-fns-timezone';
const dateNow = new Date();
const timeFormattedForUserTimezone = formatToTimeZone(dateNow, 'h:mm A z, MMM do, YYYY', {timeZone:'Europe/Berlin'})

...I get this in timeFormattedForUserTimezone : ...我在timeFormattedForUserTimezone得到这个:

2:20 AM CEST, Aug 1st, 2020 2020 年 8 月 1 日凌晨 2:20 CEST

Google disagrees and tells me the time in Berlin is:谷歌不同意并告诉我柏林的时间是:

2:23 AM August 31, 2020 2020 年 8 月 31 日凌晨 2:23

...ie Aug 31st rather than Aug 1st. ...即 8 月 31 日而不是 8 月 1 日。

What am I missing?我错过了什么?

Your time string is 'h:mm A z, MMM do, YYYY' which uses do which is Day of week .您的时间字符串是'h:mm A z, MMM do, YYYY' ,它使用doDay of week

Use 'h:mm A z, MMM Do, YYYY' instead (Note the upper case D in Do which stands for Day of month ).使用'h:mm A z, MMM Do, YYYY'代替(注意Do中的大写D代表Day of month )。

Docs here: https://github.com/prantlf/date-fns-timezone/blob/master/docs/API.md#formattotimezone文档在这里: https://github.com/prantlf/date-fns-timezone/blob/master/docs/API.md#formattotimezone

So your code becomes:所以你的代码变成:

import {formatToTimeZone } from 'date-fns-timezone';
const dateNow = new Date();
const timeFormattedForUserTimezone = formatToTimeZone(dateNow, 'h:mm A z, MMM Do, YYYY', {timeZone:'Europe/Berlin'})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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