Define date
The Date object is used to process the Date and time.
You can use the new keyword to define the Date object. The following code defines a Date object named myDate:
Var myDate = new Date ()
Note: The Date object uses the current Date and time as its initial values.
Note the last form. The parameter represents the number of milliseconds between the creation time and the GMT time of January 1, January 1, 1970. The meanings of various functions are as follows:
Month: represents the name of a month in English, from January to December.
Mth: an integer representing the month, from (January 1, January) to 11 (January 1, December)
Dd: The day of a month, from 1 to 31.
Yyyy: the year in four-digit format.
Hh: hours, from 0 (midnight) to 23)
Mm: The number of minutes, an integer from 0 to 59.
Ss: Number of seconds, an integer from 0 to 59
Ms: number of milliseconds, an integer greater than or equal to 0
The JavaScript date format generally supports the following four types:
1. ISO Dates
The YYYY-MM-DD format var d = new Date ("2016-03-25 ");
2. Long Dates
Mmm dd yyyy format var d = new Date ("Mar 25 2016 ");
3. Short Dates
MM/DD/YYYY format var d = new Date ("03/25/2016"); or var d = new Date ("2015/03/25 ");
4. Full Format
Var d = new Date ("Wed Mar 25 2016 09:56:24 GMT + 0100 (W. Europe Standard Time )");
<Script type = "text/javascript">
Var datetime = new Date ('2014/1 '); // Tue Mar 01 2016/3 00:00:00 GMT + 2016
Var datetime = new Date ('1970-03-01 '); // Tue Mar 01 2016 08:00:00 GMT + 2016
Var datetime = new Date ("Wed Mar 25 2015 09:56:24 GMT + 0100 (W. Europe Standard Time )");
Var datetime = new Date ('1970-3-1 '); // Invalid Date
// Alert (datetime );
</Script>
Operation Date
By using the date object method, we can easily operate on the date.
In the following example, we set a specific date for the date object (January 1, August 9, 2008 ):
Var myDate = new Date ()
MyDate. setFullYear (, 9)
Note: The month parameter is between 0 and 11. That is to say, if you want to set the month to August, the parameter should be 7.
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 the number of days increases, the month or year is changed, and the date object is automatically converted.
Comparison date
A date object can also be used to compare two dates.
The following code compares the current date with August 9, 2008:
Var myDate = new Date ();
MyDate. setFullYear (, 9 );
Var today = new Date ();
If (myDate> today)
{
Alert ("Today is before 9th August 2008 ");
}
Else
{
Alert ("Today is after 9th August 2008 ");
}