修改时区新日期javascript [英] Modify timezone new date javascript

查看:62
本文介绍了修改时区新日期javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个日历插件,在其中我希望Date对象与特定时区一起存储.我正在使用 moment.js moment-timezone.js .

I'm using a calendar plugin in which I want the Date object to be stored with a particular timezone. I am using the plugin, moment.js and moment-timezone.js.

console.log(新日期(moment.tz("2012-11-04 01:00:00-04:00",美国/纽约州").format()));

console.log(new Date(moment.tz("2012-11-04 01:00:00-04:00", "America/New_York").format()));

获取:Date {Sun Nov 04 2012 05:00:00 GMT+0000}

我需要:Date {Sun Nov 04 2012 05:00:00 GMT-0400}

我正在使用PhoneGap库,我需要两个对象日期.

I'm using a PhoneGap library and I need two object date.

window.plugins.calendar.createEvent(title, event_location, notes, start_date, end_date, function(message) { }, function(message) { });

该插件将事件保存在设备日历中,并检测发送的时区是否与设备时区不同,并更改事件的时区.在设备和事件的时区中显示事件的事件时间.

The plugin save the event in the device calendar and detects if the timezone that is sent is different from the device timezone and changes the timezone of the event. Showing the event time of the event in the timezone of the device and the event.

推荐答案

您的要求是不可能的. Date对象 不能显示代码运行所在的本地时区以外的任何时区.

What you asked is not possible. The Date object cannot present any time zone other than the local time zone where the code is running.

此外,如果您已经具有正确的本地日期,时间和偏移量,那么使用moment-timezone几乎没有好处.只需使用parseZone保留给出的偏移量即可:

Also, if you already have the correct local date, time, and offset, then there's little benefit of using moment-timezone. Simply use parseZone to retain the offset that was given:

console.log(moment.parseZone("2012-11-04 01:00:00-04:00").format());
// "2012-11-04T01:00:00-04:00"

这篇关于修改时区新日期javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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