Edmundo Del Gusto Edmundo Del Gusto - 1 month ago 5
Javascript Question

Javascript returns wrong Date values (NodeJS)

I'm working on a NodeJS Projects and I get wrong Date values. And I don't get what I am doing wrong.

var d = new Date(results[1].timestamp);
console.log(results[1].timestamp); // 2016-05-10T13:29:47 <- this is right (stored at my DataBase)
console.log(d.getDate()); //10
console.log(d.getFullYear()); //2016
console.log(d.getMonth()); //4
console.log(d.getDay()); //2
console.log(d.getHours()); //15
console.log(d.getMinutes()); //29
console.log(d.getSeconds()); //47


So Month, Day and Hours are wrong.
I see these results in google chrome at my Mac

Thanks for helping

Answer

A few errors here:

  1. getMonth returns a 0 based month. That is May is 04.

  2. getDay returns the day of the week. I guess you want getDate

  3. the date is parsed as UTC and getHour is according to the locale. So the hour might be different from what you want (but right here it seems to be "exact", as is it's the same value than inputted).

A tip for your next problems: Have a look at some documentation. For example the MDN.

Comments