var dateTimeNow = new Date();or you can pass the date fields to constructor:
var dateTime = new Date(year, month, day, hours, minutes, seconds, milliseconds);Date class has many useful and handy function which allow you to manipulate Date object and perform different operations. You can get day, month, year, hour from date object or you can convert it to UTC date object. Similarly you can change day, month, year or time of an object.
dt.setMonth(0); //set January dt.setMonth(1); //set February dt.setMonth(2); //set march dt.setMonth(3); //set April dt.setMonth(4); //set May dt.setMonth(5); //set June
So What will happen if you set 12 to this date object?
var dt = new Date(); dt.setFullYear(2014); dt.setMonth(12); dt.setDate(1); alert(dt.toString()) //alert January 2015Here is Demo
The inconsistent and confusing behavior I found for which I am writing this article is, setting Date to a date object.
var dt = new Date(2014,3,20); // 20th April 2014 dt.setDate(31); //will change to 1st of May dt.setMonth(9); //1st October 2014 alert(dt.toString()) //alert 1st October 2014Here is Demo
- If you are setting day of month than make sure to change month first than day.