简体   繁体   English

时刻 - 时区解析给定的时区

[英]moment-timezone parsing the given time zone

I have an issue when parsing the Date object using the moment-timezone 使用moment-timezone解析Date对象时遇到问题

The problem 问题

I am creating a nodejs app that should check the new Date() object with arbitrary time from database (and act accordingly). 我正在创建一个nodejs应用程序,该应用程序应该使用数据库中的任意时间检查新的Date()对象(并相应地执行)。 The time, and the timezone are persisted in database. 时间和时区保存在数据库中。

In example 在例子中

time | timezone
11:00| US/Eastern

When a REST call comes, I have to take new Date() object and to transform it to given timezone and see whether the current time is later than 9am. 当REST调用到来时,我必须获取新的Date()对象并将其转换为给定的时区,并查看当前时间是否晚于上午9点。 But servers timezone and persisted timezone are not the same. 但是服务器时区和持久时区并不相同。

The issue 问题

I create todays date string like this 我像这样创建今天的日期字符串

function getTodaysDate() {
    var today = new Date(),
        dd = today.getDate(),
        mm = today.getMonth()+1,
        yyyy = today.getFullYear();

    if(dd<10) {
        dd='0'+dd
    }

    if(mm<10) {
        mm='0'+mm
    }

    return yyyy +'-' + mm + '-' + dd;

}

And trying to create Timestamp object with moment-timezone 并尝试使用moment-timezone创建Timestamp对象

startTime = moment.tz(new Date(getTodaysDate() + ' ' + '11:00'), 'US/Eastern');

But the framework correctly takes the date and transforms it to US/Eastern time zone. 但框架正确地采用了日期并将其转换为美国/东部时区。

So when I print startTime.format(); 所以当我打印startTime.format();

I get 我明白了

2016-08-01T07:00:00-04:00

And I would like 而且我想

2016-08-01T11:00:00-04:00

So is there a way using moment-timezone package to set the Date and time and to just treat them as given timezone? 那么有没有办法使用moment-timezone包来设置日期和时间,并将它们视为给定的时区?

You don't need any of the Date object manipulation. 您不需要任何Date对象操作。 In theory, you should just be able to do: 从理论上讲,你应该能够做到:

var zone = 'US/Eastern'
var time = '11:00'

var result = moment.tz(time, 'HH:mm', zone).format();

However, there's a known bug with this that uses the UTC current date instead of the time zone's current date. 但是,有一个已知的错误 ,它使用UTC当前日期而不是时区的当前日期。 Until it's fixed, you have to do this instead: 在它修复之前,你必须这样做:

var zone = 'US/Eastern'
var time = '11:00'

var s = moment.tz(zone).format('YYYY-MM-DD') + ' ' + time;
var m = moment.tz(s, zone);
var result = m.format();

(this assumes your input time value is in HH:mm format) (这假设您的输入time值为HH:mm格式)

So the problem was that 所以问题是

startTime = moment.tz('here the date goes', 'US/Eastern');

Was either expecting ISO format or manually formatted (syntax of this was not known to me), or the Date() object. 是期望ISO格式还是手动格式化(我的语法不为我所知)或Date()对象。 I first tried with this 我第一次尝试这个

2016-08-01 11:00 

Moment library complained (I will post the full error message when I come to office). 时刻图书馆抱怨(我上任时会发布完整的错误信息)。

Solution

Add T. 添加T.

startTime = moment.tz('2016-08-01T11:00', 'US/Eastern');

I hate timedate libraries and how we track of time. 我讨厌时间库和我们如何追踪时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM