我有一些使用 react-intl 的客户端代码以用户定义的语言和区域(文化/区域设置)显示货币、语言和日期时间值。除了这些要求之外,我还想在使用长日期和时间格式时显示时区缩写(例如2016 年 2 月 8 日,15:02 GMT)。
为此,我尝试使用 moment-timezone模块似乎做了正确的事情 in isolation 。然而,我发现在我的应用程序中使用 react-intl 的同一行代码会生成正确的数据,但日期时间格式错误。
document.write(moment(Date()).tz("Europe/London").format('LLL z'));
预计:2016 年 2 月 8 日 20:57 GMT
实际:2016 年 2 月 8 日晚上 9:04 GMT
我的猜测是,这与我没有直接在本地 en-US 加载时刻有关,因为时刻是作为 en-US 的一部分自动加载的>react-intl 库使用以下类型的代码。
addLocaleData(en);
var intlData = {
"locales": ["en-US", "en-GB"],
"formats": {
"date": {
"short": {
"day": "numeric",
"month": "long",
"year": "numeric"
},
"longdatetime": {
"day": "numeric",
"month": "long",
"year": "numeric",
"hour": "numeric",
"minute": "numeric"
}
},
"time": {
"hhmm": {
"hour": "numeric",
"minute": "numeric"
}
}
}
};
const provider = <IntlProvider locale="en-GB" {...intlData}><MyApp /></IntlProvider>;
相关:
最佳答案
作为临时解决方法,我发现我可以专门为 moment-timezone 设置区域设置,如下所示。
import moment from 'moment-timezone'
moment.locale('en-GB');
关于javascript - 如何输出长日期时间格式,其中包括带有时刻时区和react-intl的时区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35279594/