Javascript返回错误的日期值(NodeJS)

时间:2016-05-11 11:25:48

标签: javascript node.js date

我正在研究NodeJS项目并且我得到了错误的日期值。而且我不知道我做错了什么。

 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

所以月,日和小时都错了。 我在Mac上的谷歌浏览器中看到了这些结果

感谢您的帮助

3 个答案:

答案 0 :(得分:5)

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.

答案 1 :(得分:2)

答案 2 :(得分:2)

getDay() function returns the Day of the date i.e. from sunday to saturday(0 to 6)

getMonth() function returns month frome January to December (0 to 1) so here you need to add 1 to correctly get the value

and I am afraid you misinterpreted getHours() result, because if I check for the mentioned date it gives me 13