如何从timeZone(瞬间时区)获取日期?

时间:2018-07-01 16:28:44

标签: javascript datetime momentjs

即使Moment Timezone documentation非常好,我也遇到了问题。如何简单地获取时区日期?

在IOS应用程序上,我需要从timeZone获取日期,但与运行我的应用程序的系统无关。

基本上,如果我的应用是在美国使用的,我希望能够即时获得moment()中的“欧洲/巴黎”日期。

可以使用Moment Timezone吗?

// This line is giving me the smartphone's system date
var today = moment().tz('Europe/Paris');
console.log(today.format());

// This line is giving me the smartphone's system date less one hour
var today2 = moment().tz('Etc/GMT-1');
console.log(today2.format());

// I have no clue where this date is coming from, it doesn't related to my system hour
var today3 = moment().tz('America/Los_Angeles');
console.log(today3.format());
<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-2012-2022.min.js"></script>

1 个答案:

答案 0 :(得分:0)

let today = moment().tz("America/Los_Angeles")

这应该通过调用moment()获得当前日期,并用tz更改时区。 See docs here.

要进行调试,请尝试以下方式:moment.tz既可以接受日期,也可以接受tz来将日期更改为。

let now = moment()
console.log(now)
let nowTZ = moment.tz(now, "America/Los_Angeles");
console.log(nowTZ)