-1

In a project I need to convert in javascript a timestamp into a human readable string, however the number of month was incorrect so I performed the following test in a javascript console :

Image of my test

How to explain why there is a one month difference ?

Thank you in advance

Pierre HUBERT
  • 401
  • 5
  • 15

4 Answers4

5

Javascript dates start at 0 for months

example:

  • 0: Jan
  • 1: Feb
  • 2: Mar
  • etc
Mike
  • 877
  • 10
  • 17
3

In javascript months start from 0. So January is 0, and december is 11

N. Ivanov
  • 1,777
  • 2
  • 13
  • 28
3

docs

Return value An integer number, between 0 and 11, representing the month in the given date according to local time. 0 corresponds to January, 1 to February, and so on.

Ivan Minakov
  • 1,432
  • 5
  • 12
3

Javascript's date.getMonth starts the month count from 0. So you need to add +1.

var date = new Date();
console.log(date.getMonth() + 1);
Koby Douek
  • 15,427
  • 16
  • 64
  • 91