Vipin
Vipin

Reputation: 938

Setting date in nodejs date object shows date as different value

When i'm setting up date object using the below statement the output showing different in nodejs. Can you please help me to understand why it is like this. And how i need to pass to print right value.

var date1= new Date(2017,01,01);
var date2= new Date(2017,01,31);
console.log("2017-01-01 is printed as ==>",date1);
console.log("2017-01-31 is printed as ==>",date2);

Output

2017-01-01 is printed as ==> 2017-01-31T18:30:00.000Z

2017-01-31 is printed as ==> 2017-03-02T18:30:00.000Z

Upvotes: 2

Views: 348

Answers (2)

TGW
TGW

Reputation: 835

Printing your logs like this
console.log("2017-01-01 is printed as ==>"+date1); console.log("2017-01-31 is printed as ==>"+date2); will give you your expected logs It is because console log will take date as object of date while using comma and while using + it will apply object.toString() to date object.

Upvotes: 1

SunriseM
SunriseM

Reputation: 995

Month parameter starts from 0 (January) to 11 (December) so if you want to get 2017-01-01 you need to use:

var date1 = new Date(2017, 0, 1)

More info here: https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Date

Upvotes: 0

Related Questions