将时间转换为时区中的特定时区 [英] Convert a time to a specific timezone in moment timezone

查看:101
本文介绍了将时间转换为时区中的特定时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有个约会 2018-06-19 09:06:29 .它是如何存储在数据库中的.我正在使用 moment-timezone 并调用此函数.

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');
}

在这里,我知道我的时区是美国/芝加哥",并且是"UTC-5小时".我应该把时间作为争论的时间提前5个小时.

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?

这些是我当前对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.com/expo/react-native/archive/sdk-27.0.0.tar.gz",
"react-native-swipeable": "^0.6.0",
"react-navigation": "1.5.11"

推荐答案

根据我的评论,不确定到底出了什么问题(以及它过去如何在您的身边工作),但是这对我来说应该是如何工作的:

As per my comments, 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. 使用您的日期时间创建一个时刻,并使用 moment.utc()

使用 moment.tz()

这将给出类似的内容:

function getDateString(t, tz) {
    return moment.utc(t, 'YYYY-MM-DD HH:mm:ss')
    .tz("America/Chicago")
    .format('l');
}

这篇关于将时间转换为时区中的特定时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆