artgb
artgb

Reputation: 3233

Javascript Date working strange

I was working with Javascript Date and faced strange problem.

date1 = new Date(1970, 1, 1);
date2 = new Date("1970-01-01T13:00:00.000Z");
console.log(date1.getYear());  //70
console.log(date1.getMonth()); //1
console.log(date1.getDay());   //0 expect 1
console.log(date2.getYear());  //70
console.log(date2.getMonth()); //0 expect 1
console.log(date2.getDay());   //4 expect 1

Why this result happened? What I am doing wrong with Date Object?
FIDDLE

UPDATE:

console.log(date1); 

shows this result.

Date 1970-01-31T14:00:00.000Z

Upvotes: 0

Views: 46

Answers (1)

user8811940
user8811940

Reputation:

With new Date(year, month, date), month is 0 based, so 1 is not January but february, so your date1 and date2 are different dates. Then, the function getDay returns 0 to 6, that corresponds to Monday to Sunday. If you want the date, you have to use getDate instead.

Upvotes: 3

Related Questions