I have two date strings - same month, day and time, but different year (2017, 1970). When I convert them to Date object and then use getDate()
function, I get different day.
var d1 = '1970-05-11T22:00:00.000Z';
var d2 = '2017-05-11T22:00:00.000Z';
console.log(new Date(d1), new Date(d1).getDate()); // day 11
console.log(new Date(d2), new Date(d2).getDate()); // day 12
Why is that so and how can I control this behavior?
console.log(new Date(d1) + '', new Date(d1).getDate());
andconsole.log(new Date(d2) + '', new Date(d2).getDate());
- I suspect daylight saving issue toogetDate()
converts to whatever timezone is configured in your browser. You can alternatively usegetUTCDate()
instead