Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript date looks wrong when changing the month

Tags:

javascript

My eyes must be deceiving me, but when I call setMonth(1) shouldn't the date of that date object be February? (Today is March 30, 2016)

x = new Date();
y = new Date();
x.setMonth(1);

document.write(x.toString() + '<br>' + y.toString());

Results in:

like image 513
Adam Avatar asked Mar 30 '16 14:03

Adam


People also ask

How to get time from new Date JS?

If you instead want to get the current time stamp, you can create a new Date object and use the getTime() method. const currentDate = new Date(); const timestamp = currentDate. getTime(); In JavaScript, a time stamp is the number of milliseconds that have passed since January 1, 1970.

What is Date object in JavaScript?

The Date object is an inbuilt datatype of JavaScript language. It is used to work with dates and times. The Date object is created by using new keyword, i.e. new Date(). The Date object can be used date and time in terms of millisecond precision within 100 million days before or after 1/1/1970.


1 Answers

There is no February 30, 2016

So therefore the date processor correctly changes the date to March 1, 2016

like image 153
Naftali Avatar answered Sep 19 '22 22:09

Naftali