Javascript time display code set (Date object)

Source: Internet
Author: User
Tags time and date

Javascript Time Functions
Javascript provides the Date object for time and Date calculation.

The Date object has multiple constructors:

New Date () // current time
New Date (milliseconds) // The number of milliseconds from the start time to January 1, January 1, 1970
New Date (datestring) // string represents the Date and time. This string can be converted using Date. parse (), for example, "Jannuary 1, 1998 20:13:15"
New Date (year, month, day, hours, minutes, seconds, microseconds) // time value. You do not need to write all the values. If you do not specify this value, the default value is 0.


Create a Date object first, for example:
DateObj = new Date ();

Then call the function, such
Year = dateObj. getFullYear (); // obtain the year value.


The following is a list of functions.

Retrieve class functions:
GetDate () function -- returns the number of days (1-31)
GetDay () function -- returns the number of weeks (0-6)
GetFullYear () function -- returns the four-digit year.
GetHours () function -- Return hours (0-23)
GetMilliseconds () function -- returns the number of milliseconds (0-999)
GetMinutes () function -- returns the number of minutes (0-59)
GetMonth () function -- returns the number of months (0-11)
GetSeconds () function -- number of seconds returned (0-59)
GetTime () function -- returns the timestamp representation (expressed in milliseconds)
GetYear () function -- returns the year (real year minus 1900)


Set class functions:
(The following functions return the number of milliseconds between the date object and midnight on January 1, January 1, 1970)
SetDate () function -- set the day of the month
SetFullYear () function -- set the year, month, and day
SetHours () function-set hours, minutes, seconds, and milliseconds
SetMilliseconds () function -- set the number of milliseconds
SetMinutes () function-set minute, second, millisecond
SetMonth () function -- set month, day
SetSeconds () function -- set the day of the month
SetTime () function -- set the date object in milliseconds
SetYear () function -- set the year (real year minus 1900)


Convert display class functions:
ToLocalString () function -- returns the localized string representation.
ToLocaleDateString function -- returns the localized string of the date part.
ToLocaleTimeString function -- returns the localized string of the time part.

Compared with local output, there are:

ToString ()
ToDateString ()
ToTimeString ()

The difference is that the former has different local language formats based on different machines, and the latter is an internal representation format.


Date Parsing Functions
Date. parse () function-parses a Date string and returns the number of milliseconds between the Date and midnight on January 1, January 1, 1970.

For more information about the time zone, see the javascript time zone function.

Javascript display time instance
<Html> <pead> </pead> <body onload = "startTime ()"> </body> </ptml>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
JavaScript display system time with date
JavaScript displays the system time, displays the year, month, day, hour, minute, second, and week, automatically refreshes every second, and adds the date color difference:
Black from Monday to Friday
Green on Saturday
Red on Sunday
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <ptml xmlns = "http://www.w3.org/1999/xhtml"> <pead> <meta http-equiv =" content-Type "content =" text/html; charset = gb2312 "/> <title> JavaScript time display </title> </pead> <body> </ptml>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

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.