Javascript returns wrong Date values (NodeJS)

后端 未结 3 599
醉话见心
醉话见心 2021-01-29 09:56

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         


        
相关标签:
3条回答
  • 2021-01-29 10:09

    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.

    0 讨论(0)
  • 2021-01-29 10:11

    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 讨论(0)
  • 2021-01-29 10:24

    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.

    0 讨论(0)
提交回复
热议问题