1

I'm creating a new Date() with the current time and setting its hours to 0. Until some days ago everything was fine, but now its getting 23:00 from the past day instead 00:00 from the current.

Does anyone know why?

The code can be found here https://jsfiddle.net/f1kshewz/2/

Output:

Sat Oct 17 2015 23:00:00 GMT-0300 (BRT)

henriale
  • 877
  • 8
  • 18
  • Daylight saving time. I would recommend you read this: http://stackoverflow.com/questions/16946002/javascript-time-zone-is-wrong-for-past-daylight-saving-time-transition-rules – Iago Oct 18 '15 at 08:58

2 Answers2

4

Check your timezone with this code :

var d = new Date()
var n = d.getTimezoneOffset();
console.log(n); // minutes

And make UTC-time like this :

new Date(Date.UTC(year, month, day, hour, minute, second))
A-312
  • 10,203
  • 4
  • 38
  • 66
3

Based on your time offset I'm assuming you're in Brazil? So it looks like daylight saving meant your local time went forward an hour in relation to GMT at midnight last night http://www.timeanddate.com/time/change/brazil/brasilia?year=2015

I've found moment.js extremely helpful when working with dates and times : http://momentjs.com/

atomless
  • 1,244
  • 14
  • 15
  • Thanks @atomless for helping! Yes, it does is daylight saving time, for that reason I could simply solve the problem by adding a condition on setup. And about the library, I'm actually using that one ;) Thanks – henriale Oct 18 '15 at 10:19
  • @AlexandreAraujo glad I could help. If you used the moment timezone library you'd not need to worry about a conditional as it will handle that for you : http://momentjs.com/timezone/docs/ – atomless Oct 18 '15 at 10:38