Ivan TImofeev
Ivan TImofeev

Reputation: 21

Javascript getDate()and getMonth() return wrong result

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?

Upvotes: 1

Views: 2908

Answers (1)

Simon
Simon

Reputation: 2423

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.

Upvotes: 1

Related Questions