Javascript 在不同的计算机时区上使用具有相同参数的formatDate时,为什么react intl不稳定?
我从服务器上得到一个时间戳Javascript 在不同的计算机时区上使用具有相同参数的formatDate时,为什么react intl不稳定?,javascript,reactjs,internationalization,momentjs,react-intl,Javascript,Reactjs,Internationalization,Momentjs,React Intl,我从服务器上得到一个时间戳 const date = 1420070399999 在控制台中产生: 新日期(日期)=>2014年12月31日星期三23:59:59 GMT+0000(UTC) 然后对其应用以下排列(此处矩为moment@2.22.1和intl作为属性使用injectIntl来自react intlpackage`): 当我的电脑时区设置为美国托莱多(中美洲时间)时,这些变量(从第二个开始,不包括第一个)具有以下值: "December 2014" "12/31/2014" "1
const date = 1420070399999
在控制台中产生:
新日期(日期)=>2014年12月31日星期三23:59:59 GMT+0000(UTC)
然后对其应用以下排列(此处矩为moment@2.22.1
和intl
作为属性使用injectIntl
来自react intl
package`):
当我的电脑时区设置为美国托莱多(中美洲时间)时,这些变量(从第二个开始,不包括第一个)具有以下值:
"December 2014"
"12/31/2014"
"11/30/2014"
"December 2014"
"November 2014"
但当我切换到日本东京时,我得到了以下信息:
"December 2014"
"1/1/2015"
"12/1/2014"
"January 2015"
"December 2014"
如你所见,托莱多有12月/11月,东京有1月/12月
为什么会这样?我发现问题在于,我正在向formatDate
方法(例如2014年12月
)提供月/年字符串,该方法在运行时浏览器时区中消化为2014年12月午夜,所以如果我在东京,受人尊敬的UTC当地时间月份变为11月,因为它位于东京之后
我应该使用timeZone
选项,作为第二个参数传递给intl.formatDate()
方法,如键入DateTimeFormatOptions…
声明中所述。这将迫使react intl
基础intl
JS-API方法formatDate
将提供的日期作为第一个参数处理为UTC
const momentSimple = moment.utc(date);
const momentFormatted = momentSimple.format(dateFormat);
const defaultFormatSimple = intl.formatDate(momentSimple, { timeZone: 'UTC' });
const defaultFormatFormatted = intl.formatDate(momentFormatted, { timeZone: 'UTC' });
const formattedFormatSimple = intl.formatDate(momentSimple, { year: 'numeric', month: 'long', timeZone: 'UTC' });
const formattedFormatFormatted = intl.formatDate(momentFormatted, { year: 'numeric', month: 'long', timeZone: 'UTC' });
const momentSimple = moment.utc(date);
const momentFormatted = momentSimple.format(dateFormat);
const defaultFormatSimple = intl.formatDate(momentSimple, { timeZone: 'UTC' });
const defaultFormatFormatted = intl.formatDate(momentFormatted, { timeZone: 'UTC' });
const formattedFormatSimple = intl.formatDate(momentSimple, { year: 'numeric', month: 'long', timeZone: 'UTC' });
const formattedFormatFormatted = intl.formatDate(momentFormatted, { year: 'numeric', month: 'long', timeZone: 'UTC' });