Summary of JavaScript date object usage
Create a javascript Date object
Create a date object: Var objDate = new Date ([arguments list]); I have summarized the following parameters: New Date (month dd yyyy hh: mm: ss ); // The following hh: mm: ss is optional (if not selected, it is the default start time), and the first three steps can be random, or even each field can be followed by a comma New Date (yyyy, mth, dd, hh, mm, ss ); // Except the first two fields (year and month), the remaining fields are optional (if not selected, the default value is start). However, it is best not to change the order here. New Date (MS ); // The parameter indicates 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 |
Date in JavaScript)
<Script type = text/javascript>
Document. write (Date ())
</Script>
------------------------
Example: getTime () function
Use the getTime () function to calculate the number of years that have elapsed since January 1, 1970
<Script type = text/javascript>
Var minutes = 1000*60
Var hours = minutes * 60
Var days = hours * 24
Var years = days * 365
Var d = new Date ()
Var t = d. getTime ()
Var y = t/years
Document. write (It's been: + y + years since 1970/01/01 !)
</Script>
------------------------
Example: setFullYear ()
Use the setFullYear () function to set a date.
<Script type = text/javascript>
Var d = new Date ()
D. setFullYear (1992,10, 3)
Document. write (d)
</Script>
-------------------------
Example: function toUTCString ()
Use the toUTCString () function to convert today's date to a string
<Script type = text/javascript>
Var d = new Date ()
Document. write (d. toUTCString ())
</Script>
--------------------------
Example: getDay () function ()
Use the getDay () function and an array to write the day of the week
<Script type = text/javascript>
Var d = new Date ()
Var weekday = new Array (7)
Weekday [0] = Sunday
Weekday [1] = Monday
Weekday [2] = Tuesday
Weekday [3] = Wednesday
Weekday [4] = Thursday
Weekday [5] = Friday
Weekday [6] = Saturday
Document. write (Today it is + weekday [d. getDay ()])
</Script>
---------------------------
Example: display a clock
<Script type = text/javascript>
Function startTime ()
{
Var today = new Date ()
Var h = today. getHours ()
Var m = today. getMinutes ()
Var s = today. getSeconds ()
// Add a zero in front of numbers <10
M = checkTime (m)
S = checkTime (s)
Document. getElementById ('txt '). innerHTML = h ++ m ++: + s
T = setTimeout ('starttime () ', 500)
} Function checkTime (I)
{
If (I <10)
{I = 0 + I}
Return I
}
</Script>
(