JS get the current date time and refresh the sample regularly _javascript tips

Source: Internet
Author: User
Tags setinterval

JS Get current Date Time

var date = new Date (); 
Date.getyear (); Get the current year (2-bit) 
date.getfullyear ();//Get Full year (4-bit, 2014) 
date.getmonth ()//Get Current month (0-11, 0 for January) 
Date.getdate (); Get the current day (1-31) 
date.getday ();//Get Current week x (0-6, 0 for Sunday) 
date.gettime ()//Get current time (milliseconds from 1970.1.1) 
Date.gethours (); Gets the current number of hours (0-23) 
date.getminutes ();//Get the current number of minutes (0-59) 
date.getseconds ();//Get current seconds (0-59) 
Date.getmilliseconds (); Gets the current number of milliseconds (0-999) 
date.tolocaledatestring ();//Get the current date as June 25, 2014 
date.tolocaletimestring ();//Get the current time as PM 4:45:06 
date.tolocalestring ()//Get date and time such as June 25, 2014 4:45:06

Note: both getyear () and getFullYear () can get the year, but the two are slightly different

GetYear () displayed in the browser is: 114 (in 2014 years for example), because GetYear returns the current year-1900 value (that is, the year Base is 1900)

Use JS to get the year is used: getFullYear ()

Timed Refresh

Timed refresh uses setinterval, specific settimeout and setinterval to refer to other information.

1, first page needs a region to display time

<div id= "Showdate" ></div>

2. Get time

<script type= "Text/javascript" > 
$ (function () { 
setinterval ("GetTime ();", 1000);///Every second of the execution 
}) 
//Get system Current time 
function GetTime () { 
var mydate = new Date (); 
var date = mydate.tolocaledatestring (); 
var hours = mydate.gethours (); 
var minutes = Mydate.getminutes (); 
var seconds = mydate.getseconds (); 
$ ("#showDate"). HTML (date+ "+hours+": "+minutes+": "+seconds"); Assign value to div 
} 
</script>

Use toLocaleDateString () to get the date, no need to get the year, month, day alone

and tolocaletimestring () can be directly obtained minutes and seconds, because it gets the format is not needed. It can then be obtained separately.

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.