The date object is used to process the date and time.
Javascript date object instance
-
Returns the date and time of the current day.
-
How to Use the date () method or the date of the current day.
-
Gettime ()
-
Use gettime () to calculate the number of years from January 1, 1970 to today.
-
Setfullyear ()
-
How to Use setfullyear () to get the exact date.
-
Toutcstring ()
-
How to Use toutcstring () to convert the date of the current day (based on UTC) to a string.
-
Getday ()
-
How to Use getday () to display the week, not just a number.
-
Display a clock
-
How to display a clock on a webpage.
Complete date object reference manual
We provide the Javascript date object reference manual, which includes all the attributes and methods that can be used for date objects.
This Manual contains a detailed description of each attribute and method and related examples.
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.
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(2008,7,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(2008,7,9);var today = new Date();if (myDate>today){alert("Today is before 9th August 2008");}else{alert("Today is after 9th August 2008");}