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
Add a ment  | 

1 Answer 1

Reset to default 4

As 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 :

  1. Create a moment with your date time, specifying that this is expressed as utc, with moment.utc()

  2. 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