1

I create a new Date object, using timestamp. If I print out that object, it returns correct date and time. But if I try to use getDate()and getTime(), they get me back wrong numbers.

My code:

var textDate = new Date(timestamp);

console.log(timestamp);
console.log(textDate);  
console.log(textDate.getDate(),textDate.getMonth(),textDate.getFullYear());

My console result:

1476483081000
Date 2016-10-14T22:11:21.000Z
15 9 2016

How can I get correct date and month from textDate variable?

Ivan TImofeev
  • 21
  • 1
  • 3
  • 1
    You are getting the correct numbers, months are zero based, so october would actually be `9`, and the date is probably adjusted for timezone – adeneo Oct 14 '16 at 20:09
  • Duplicate:[javascript date to string](http://stackoverflow.com/questions/5914020/javascript-date-to-string) – jedi Oct 14 '16 at 20:12
  • Month is correct. The problem with date was resolved after I use getUTCDate() and getUTCHours(). Thank you! – Ivan TImofeev Oct 14 '16 at 20:28

1 Answers1

1

The getMonth() method returns the month from 0 to 11.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMonth

When you print the date, it relies on the timezone. If you are in a timezone 2 hours away from GMT, then the 22:11 might shift to a new day, this is probably why getDate() returns the dext day.

Simon
  • 2,353
  • 1
  • 13
  • 28