christian
christian

Reputation: 177

momentjs timezone delivers inconsistent hours

I am using moment-timezone in my nodejs/express application on the server side. The problem ist that the tz() function returns inconsistent results and I don't understand why. Here's what I am doing:

items.forEach(function(item){
 moment(item.start.date).hour(5).minute(0).tz(Europe/Vienna).format()
})

(item.start.date comes in google date format(not dateTime format), like this: 2019-03-31) And it returns sometimes +1 hour and sometimes +2 hours like this:

2018-05-20T07:00:00+02:00
2018-08-15T07:00:00+02:00
2018-12-25T06:00:00+01:00
2018-12-26T06:00:00+01:00
2018-12-08T06:00:00+01:00
2019-01-01T06:00:00+01:00
2019-01-06T06:00:00+01:00
2018-05-21T07:00:00+02:00
2018-05-31T07:00:00+02:00
2018-11-01T06:00:00+01:00
2018-11-02T06:00:00+01:00
2018-12-02T06:00:00+01:00
2018-12-31T06:00:00+01:00
2018-10-26T07:00:00+02:00
2019-04-14T07:00:00+02:00
2019-04-22T07:00:00+02:00
2019-05-01T07:00:00+02:00
2019-04-19T07:00:00+02:00
2019-04-21T07:00:00+02:00
2018-10-28T06:00:00+01:00
2019-03-31T07:00:00+02:00

Your help is very much appreciated! Thanks a lot!

Upvotes: 1

Views: 88

Answers (1)

Vikasdeep Singh
Vikasdeep Singh

Reputation: 21756

You are getting +1 and +2 hours due to Daylight Savings time. To parse DST time you can check this link:

https://momentjs.com/timezone/docs/#/using-timezones/parsing-ambiguous-inputs/

Just in case, to reconfirm if above date you are getting is in Daylight Saving format, you can use isDST() function of momentjs. Below is one example:

let isDaylightSaving = moment("2018-05-20T07:00:00+02:00").isDST();
console.log(isDaylightSaving);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.js"></script>

Upvotes: 1

Related Questions