Date Created
the Date object is used to process dates and times.
You can define a Date object by using the New keyword. The following code defines a Date object named MyDate:
There are four ways of initializing the date:
New Date ()//current date and time
new Date (milliseconds)//returns the number of milliseconds from January 1, 1970 to date new date (
datestring)
new Date (year, MO Nth, day, hours, minutes, seconds, milliseconds)
Most of the parameters above are optional, and in the case of unspecified, the default parameter is 0.
Some examples of instantiating a date:
var today = new Date ()
var d1 = new Date ("October, 1975 11:13:00")
var d2 = new Date (79,5,24)
var d3 = new Date (79,5,24,11,33,0)
Set Date
by using a method for date objects, we can easily manipulate dates.
In the following example, we set a specific date for the Date object (January 14, 2010):
var mydate=new Date ();
Mydate.setfullyear (2010,0,14);
In the following example, we set the Date object to the date after 5 days:
var mydate=new Date ();
Mydate.setdate (Mydate.getdate () +5);
Note: If increasing the number of days changes the month or year, the date object automatically completes the conversion.
Two date comparisons
Date objects can also be used to compare two dates.
The following code compares the current date with the January 14, 2100:
var x=new Date ();
X.setfullyear (2100,0,14);
var today = new Date ();
if (x>today)
{
alert ("Today is before 14th January 2100");
}
else
{
alert ("Today was after 14th January 2100");
}
How to get the date of the day using the date () method.
Source code:
Test results:
Sat Oct 2015 15:14:48 gmt+0800 (China Standard Time)