最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Javascript returns wrong Date values (NodeJS) - Stack Overflow

programmeradmin0浏览0评论

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

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

Share Improve this question asked May 11, 2016 at 11:25 Edmundo Del GustoEdmundo Del Gusto 3881 gold badge4 silver badges16 bronze badges 1
  • 2 Did you read the documentation for getDay and getMonth ? I don't think you did... – Denys Séguret Commented May 11, 2016 at 11:27
Add a ment  | 

3 Answers 3

Reset to default 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.

The getDay() method returns the day of the week (from 0 to 6) for the specified date.

The getMonth() method returns the month (from 0 to 11) for the specified date, according to local time.

The getHours() method returns the hour (from 0 to 23) of the specified date and time.

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

发布评论

评论列表(0)

  1. 暂无评论