JavaScript time display Code collection (Date Object) _ Time Date

Source: Internet
Author: User
Tags getdate time and date
JavaScript Time Functions
JavaScript provides a Date object for calculations of time and date.

The Date object has several constructors:

New Date ()//Current time
Number of milliseconds for new Date (milliseconds)//distance start January 1, 1970
The date and time represented by the new date (datestring)//String. This string can be converted using date.parse () such as "Jannuary 1, 1998 20:13:15"
New Date (year, month, day, hours, minutes, seconds, microseconds)//time value, which can be written without all, not written, defaults to 0


When used, create a Date object, such as:
Dateobj=new Date ();

Then call the function, such as
Year=dateobj.getfullyear ()//Get year value


The following is a list of functions.

To get the class function:
GetDate () function--Return days (1-31)
Getday () function--Returns the number of weeks (0-6)
getFullYear () function--returns four-digit years
GetHours () function--Returns the number of 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 notation (in milliseconds)
GetYear () function--return year (real year minus 1900)


To set a class function:
(The following functions return the number of milliseconds between the date objects from midnight January 1, 1970)
Setdate () function--set the day of the month
setFullYear () function--set year, month, and day
Sethours () function--set hours, minutes, seconds, and milliseconds
Setmilliseconds () function--set the number of milliseconds
Setminutes () function--set minutes, seconds, milliseconds
Setmonth () function--Set month, day
Setseconds () function--set the day of the month
SetTime () function--use milliseconds to set the Date object
Setyear () function--set year (real year minus 1900)


convert display class function:
Tolocalstring () function--Returns a localized string representation
tolocaledatestring Function--Returns the localized string for the date part
tolocaletimestring function--a localized string that returns the time part

Relative to the local output, there are:

ToString ()
toDateString ()
toTimeString ()

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


date resolution class function
Date.parse () function--parses a string of dates and returns the number of milliseconds between midnight of January 1, 1970

Time zone related parts see JavaScript time zone functions

JavaScript shows time instances
<ptml> <pead> <script type= "Text/javascript" > Function StartTime () {var today=new Date () var h=to Day.gethours () var m=today.getminutes () var s=today.getseconds ()///When the number is less than 10, add 0 to the front. Look specification M=checktime (m) s=checktime (s) document.getElementById (' txt '). innerhtml=h+ ":" +m+ ":" +s t=settimeout (' StartTime () ', 1000)} function Checktime (i) {if (i<10) {i= "0" + i} return i} </script> </pead> <body onload= "StartTime ()" > <div id= " TXT "></div> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

JavaScript shows system time with date
JavaScript Displays the system time, showing the date of day, seconds and weeks, automatically refreshing every second, and adding dates to the color difference:
From Monday to Friday, it appears as black
Saturday shown as Green
Sunday shown as Red
<! 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> <span id=localtime> </span> <script type= "Text/javascript" > Function Showlocale (OBJD) {var str,colorhead,colorfoot; var yy = Objd.getyear (); if (yy<1900) yy = yy+1900; var MM = Objd.getmonth () +1; if (mm<10) mm = ' 0 ' + mm; var dd = objd.getdate (); if (dd<10) dd = ' 0 ' + dd; var hh = objd.gethours (); if (hh<10) hh = ' 0 ' + hh; var mm = Objd.getminutes (); if (mm<10) mm = ' 0 ' + mm; var ss = Objd.getseconds (); if (ss<10) ss = ' 0 ' + ss; var ww = objd.getday (); if (ww==0) colorhead= "<font color=\" #FF0000 \ ">"; if (ww > 0 && ww < 6) colorhead= "<font color=\" #373737 \ ">"; if (WW==6) colorhead= "<font color=\" #008000 \ ">"; if (ww==0) ww= "Sunday"; if (ww==1) ww= "Monday"; if (ww==2) ww= "Tuesday"; if (ww==3) ww= "Wednesday"; if (ww==4) ww= "Thursday"; if (ww==5) ww= "Friday"; if (ww==6) ww= "Saturday"; colorfoot= "</font>" str = colorhead + yy + "-" + mm + "-" + dd + "" + hh + ":" + MM + ":" + SS + "" + ww + Colorf Oot return (str); function tick () {var today; Today = new Date (); document.getElementById ("LocalTime"). InnerHTML = Showlocale (today); Window.settimeout ("tick ()", 1000); } tick (); </script> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
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.