moment tz 转换字符串
moment tz convert string
这应该很简单,但我花了几个小时查看文档,但仍然一无所获。
我有这个字符串:
var time = "2018-09-29 23:50:21"
这个时间是UTC时间,虽然没有这么说,但我知道是。
我想使用 .tz
将其转换为 PST (America/Los_Angeles)
我试过了:
moment(time).tz('America/Los_Angeles').format(MomentDefaults.DateTime);
我得到"Sep 29, 2018, 23:50"
我尝试了很多不同的组合,但都以不同的方式失败了。
我希望能够打印 "Sep 29, 2018, 16:50" 这是 time
在 utc 中转换为 pst。
我错过了什么?
您可以将 time
字符串更改为实际有效的 ISO UTC 字符串吗?
var time = "2018-09-29T23:50:21.000Z"
console.log(moment(time).tz('America/Los_Angeles').format()) //16:50
console.log(moment(time).tz('America/New_York').format()) //19:50
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.21/moment-timezone-with-data.min.js"></script>
确保您已加载时区数据等 per the docs:
In Node.js, all the data is preloaded. No additional code is needed
for loading data.
When using Moment Timezone in the browser, you will need to load the
data as well as the library.
这应该很简单,但我花了几个小时查看文档,但仍然一无所获。 我有这个字符串:
var time = "2018-09-29 23:50:21"
这个时间是UTC时间,虽然没有这么说,但我知道是。
我想使用 .tz
将其转换为 PST (America/Los_Angeles)我试过了:
moment(time).tz('America/Los_Angeles').format(MomentDefaults.DateTime);
我得到"Sep 29, 2018, 23:50"
我尝试了很多不同的组合,但都以不同的方式失败了。
我希望能够打印 "Sep 29, 2018, 16:50" 这是 time
在 utc 中转换为 pst。
我错过了什么?
您可以将 time
字符串更改为实际有效的 ISO UTC 字符串吗?
var time = "2018-09-29T23:50:21.000Z"
console.log(moment(time).tz('America/Los_Angeles').format()) //16:50
console.log(moment(time).tz('America/New_York').format()) //19:50
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.21/moment-timezone-with-data.min.js"></script>
确保您已加载时区数据等 per the docs:
In Node.js, all the data is preloaded. No additional code is needed for loading data.
When using Moment Timezone in the browser, you will need to load the data as well as the library.