Summary of JavaScript date object usage

Source: Internet
Author: User

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>

 

 

 

 

 

 

 

(

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.