admin管理员组文章数量:1415467
I have a date 2018-06-19 09:06:29
. It is how it is stored in the database. I am using moment-timezone
and calling this function.
function getDateString(t, tz) {
return moment()
.tz("America/Chicago")
.format('l');
}
Here I know that my timezone
is "America/Chicago" and it is "UTC-5 hours". I should get the time 5 hours before the time that I have passed as an argument.
This function was working perfectly until I upgraded my react. Can anyone know what might be the issue?
These are my current settings of react
"expo": "^27.0.0",
"moment": "^2.22.2",
"react": "16.3.1",
"react-moment": "^0.7.0",
"moment-timezone": "^0.5.17",
"react-native": ".0.0.tar.gz",
"react-native-swipeable": "^0.6.0",
"react-navigation": "1.5.11"
I have a date 2018-06-19 09:06:29
. It is how it is stored in the database. I am using moment-timezone
and calling this function.
function getDateString(t, tz) {
return moment()
.tz("America/Chicago")
.format('l');
}
Here I know that my timezone
is "America/Chicago" and it is "UTC-5 hours". I should get the time 5 hours before the time that I have passed as an argument.
This function was working perfectly until I upgraded my react. Can anyone know what might be the issue?
These are my current settings of react
"expo": "^27.0.0",
"moment": "^2.22.2",
"react": "16.3.1",
"react-moment": "^0.7.0",
"moment-timezone": "^0.5.17",
"react-native": "https://github./expo/react-native/archive/sdk-27.0.0.tar.gz",
"react-native-swipeable": "^0.6.0",
"react-navigation": "1.5.11"
Share
Improve this question
edited Jun 19, 2018 at 12:14
Pac0
23.3k9 gold badges73 silver badges83 bronze badges
asked Jun 19, 2018 at 10:27
Ali ZiaAli Zia
3,8755 gold badges33 silver badges82 bronze badges
2
-
"This function was working perfectly" it seems to me that this function is not doing anything with what I guess is your date/time argument
t
– Pac0 Commented Jun 19, 2018 at 11:12 - also, please give exact output you get, and the one you used to get, there are often some confusions about what a date time is as data, and how it is represented as string / message, with all the fancy timezone and locale stuff – Pac0 Commented Jun 19, 2018 at 11:14
1 Answer
Reset to default 4As per my ments, not sure exactly what is going wrong (and how it used to work on your side), but here is how it should work to me :
Create a moment with your date time, specifying that this is expressed as utc, with
moment.utc()
convert it to your timezone with
moment.tz()
This would give something like :
function getDateString(t, tz) {
return moment.utc(t, 'YYYY-MM-DD HH:mm:ss')
.tz("America/Chicago")
.format('l');
}
本文标签: javascriptConvert a time to a specific timezone in moment timezoneStack Overflow
版权声明:本文标题:javascript - Convert a time to a specific timezone in moment timezone - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745156070a2645178.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论