I have an issue when parsing the Date object using the moment-timezone
I am creating a nodejs app that should check the new Date() object with arbitrary time from database (and act accordingly). 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. But servers timezone and persisted timezone are not the same.
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
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();
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?
The data for Moment Timezone comes from the IANA Time Zone Database . New versions are released periodically as time zone laws change in various countries. The versions are named after the year and an incrementing letter. 2014a 2014b 2014c... In order to keep versions together, Moment Timezone has a bundled object format as well.
moment.tz (..., String) does parsing in given time zone It takes all the same arguments as the moment constructor, but uses the last argument as a time zone identifier:
For example, if you parse the string '2020/01/02' and then call the moment#tz () method, you're telling moment to parse the string in the locale time, and then convert the date to a given IANA timezone.
For example, date formatting: However, moment-timezone adds a moment.tz () that you can use to create a new moment object with a custom timezone. For example, here's how you can create a moment object representing the current time in Longyearbyen, Norway. The string 'Arctic/Longyearbyen' is an IANA timezone name .
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. 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).
Add T.
startTime = moment.tz('2016-08-01T11:00', 'US/Eastern');
I hate timedate libraries and how we track of time.
You don't need any of the Date
object manipulation. 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. 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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With