momentjs toDate() - 时区被重置 [英] momentjs toDate() - timezone gets reset
问题描述
我正在使用momentjs并使用 convertedDate = moment()。utcOffset(timezone).format()
将日期转换为不同的时区。这很好但是它是一个字符串,我需要将它转换为日期对象。
I am working with momentjs and converting dates to different time zones using convertedDate = moment().utcOffset(timezone).format()
. This works well but it is a string and I need to transform it to date object.
我试过新日期(convertedDate)
和 moment()。utcOffset(时区).toDate()
但是将当前时区作为日期对象返回。如何保留转换后的时区?
I've tried new Date(convertedDate)
and moment().utcOffset(timezone).toDate()
but that returns my current timezone as a date object. How can I keep the converted timezone?
推荐答案
所以我不是很远。格式需要排除时区才能工作。这段代码终于解决了我需要它的方式。
So I wasn't very far off. The format needs to exclude timezone for it to work. This code finally worked how I needed it to.
convertedDate = new Date(moment()。utcOffset(' - 4')。format( 'YYYY-MM-DD HH:mm'));
这篇关于momentjs toDate() - 时区被重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!