moment-timezones.js,如何将特殊时区中的日期转换为utc,而不考虑我的本地时区?
我的系统使用时区UTC + 03:00
my system uses timezone UTC+03:00 ,
im尝试获取字符串格式的日期,以NY时区
表示并进行转换到utc中的Date对象
im trying to get a date in string format, represented by NY timezone, and convert it to a Date object in utc
const dateInNY = moment.tz(xlsxDate, "M/D/YYYY h:mm a", "America/New_York")
.tz("Z").toDate();
不能正常工作
我如何甚至假设要转换为UTC时间?
how am i even suppose to convert to utc time?
----------- edit ---- -----------
-----------edit---------------
我使用时区 Africa / Accra使它正常工作,其中UTC偏移为0,而没有夏令时:
i got it to work, using the timezone "Africa/Accra" , where UTC offset is 0, and ther is no daylight savings time:
moment.tz(xlsxDate, "M/D/YYYY h:mm a", "America/New_York").tz("Africa/Accra")
但这是一个不好的解决方法,并且如果阿克拉政府决定更改时间法,将停止工作!
but this solution is a bad workaround, and if the government of Accra decide to change the time laws, will stop working!
有没有办法将utc时区的utc偏移设置为0?
is there a way to set the utc offset to 0 in momentjs-timezones?
我发现了一个可以完成我想做的功能,它属于momentjs库本身: utcOffset (n)
将偏移量设置为n。
i found a function that does what i was trying to do, it belongs to the momentjs library itself: utcOffset(n)
sets the offset to n.
(感谢VincenzoC,我还必须正确地正确写出日期字符串格式)
(i also had to explicitly write the date string format correctly, thanks VincenzoC)
这是我正在尝试的代码写道:
this is the code i was trying to write:
const dateInNY = moment.tz(xlsxDate, "M/D/YYYY h:mm a", "America/New_York");
const dateUTC = dateInNY.utcOffset(0).toDate();
但是,toDate函数仍然将时区更改为我的本地时区,因此 .utcOffset(0)
是多余的,我可以这样使用一下时刻:
however, the toDate function changes the timezone to my local timezone anyway, so .utcOffset(0)
is redundat, and i can just use moment this way:
const dateInNY = moment.tz(xlsxDate, "M/D/YYYY h:mm a", "America/New_York");
const dateUTC = dateInNY.toDate();
,然后将Date对象的日期更改为utc时间(在我的情况下,是JSON.stringify的东西,以后再使用对我有用)
and change the Date objects date to utc time later (in my case, the JSON.stringify stuff i use later does that for me)