javascript| Functions | Tutorials The Date object can be created using the date () constructor, and we have introduced the date () constructor in the previous tutorial, and there is no repetition here. It has no parameters, and the returned value is the current date. The following table shows the valid input for the date builder:
var today = new Date (); |
Returns the current date and time |
var newyear = new Date ("December 31, 1998 23:59:59"); |
Enter a string for the form "month, year, minute: SEC" |
var bday = new Date (75, 1, 16); |
parameter is year, month, day |
var bday = new Date (75, 1, 16, 12, 0, 0); |
The parameters are year, month, day, hour, minute, second |
Here to add: The month is starting from 0, such as January = 0, February = 1, March =3 and so on.
As you can see from the above, creating a Date object is relatively simple, and the following table is a series of functions that can be used to change or access properties of these objects:
Date Access method
Method (Methods) |
Description (description) |
Value (value) |
GetYear () |
Returns the last two digits of the year |
2001 |
GetMonth () |
Returns the month ordinal of the year (0 to 11) |
5 |
GetDate () |
Returns the day ordinal of a month (1 to 31) |
2 |
Getday () |
Returns the day ordinal of the week (0 to 6) |
6 |
getTimezoneOffset () |
Difference in average time between returning local time and Greenwich Observatory |
-480 ( -8h) |
GetHours () |
Returns the number of hour points in a day (0 to 23) |
16 |
Getminutes () |
Back to minutes (0..59) |
8 |
Getseconds () |
Returns the second of the time (0 to 59) |
24 |
GetTime () |
Returns the number of milliseconds since A.D. January 1, 1970 |
991469304470 |
Note here: Some versions of IE browsers return timezoneoffset values using the wrong symbols, such as "-" instead of "+" and so on.
Date Setting method
Setdate () |
Set the day ordinal of a month (from 0 to 30) |
Sethours () |
Set hours (from 0 to 23) |
Setminutes () |
Set minutes (from 0 to 59) |
Setmonth () |
Set month (from 0 to 11) |
Setseconds () |
Set the number of seconds (from 0 to 59) |
SetTime () |
Set time (number of milliseconds since A.D.) |
Setyear () |
Set year |
Other Date methods
Parse |
Conversion date string is the number of milliseconds since A.D., such as Date.parse ("date string") |
ToString () |
Sat June 2 16:08:24 utc+0800 2001 |
toGMTString () |
Sat, 2 June 2001 08:08:24 UTC |
toLocaleString () |
June 2, 2001 16:08:24 |
All of these functions refer to a separate date object. If you have a deep Java programming background, you can think of them as a public method of the date class. Here is a typical example to set the Date object to the current time plus 1 years:
var nextyear = new Date (); Initializing Date objects
Nextyear.setyear (Nextyear.getyear () + 1); Increased by 1 years
In fact, the parse function is a method of the date object, rather than a separate date variable, which, if used in Java terms, is called a static method of the date class. This is why we use Date.pase () instead of using Somedate.parse ().
Click
hereTo test the page