JS Gets the current date time and refreshes periodically

Source: Internet
Author: User

JS Gets the current date time

var date = new Date ();d ate.getyear ();        Gets the current year (2-bit) date.getfullyear ();    Get the full year (4-bit) date.getmonth ();       Get the current month (0-11, 0 for January) date.getdate ();        Get current day (1-31) date.getday ();         Get Current week x (0-6, 0 for Sunday) date.gettime ();        Gets the current time (the number of milliseconds since 1970.1.1) date.gethours ();       Gets the current number of hours (0-23) date.getminutes ();     Gets the current number of minutes (0-59) date.getseconds ();     Gets the current number of seconds (0-59) date.getmilliseconds ();    Gets the current number of milliseconds (0-999) date.tolocaledatestring ();     Get the current date   as of June 25, 2014 date.tolocaletimestring ();     Get current time   such as PM 4:45:06date.tolocalestring ();         Get date and time as June 25, 2014 4:45:06
Note: getYear () and getFullYear () are able to get the year, but the two are slightly different

GetYear () is displayed in the browser as: 114 (2014 for example), because GetYear returns the value of "Current year 1900" (that is, the year Base is 1900)

Use JS to get the year used: getFullYear ()


Timed Refresh
Timed refresh use SetInterval, detailed settimeout and setinterval differences for other information.

1, the first page needs a region for display time

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

2. Get Time

<script type= "Text/javascript" >$ (function () {setinterval ("getTime ();", 1000);//Run once every second})//Get system Current time function GetTime () {var mydate = new Date (), var date = mydate.tolocaledatestring (); var hours = Mydate.gethours (); var minutes = Mydat E.getminutes (); var seconds = Mydate.getseconds (); $ ("#showDate"). HTML (date+ "" +hours+ ":" +minutes+ ":" +seconds "); Assigning a value to Div}</script>

use toLocaleDateString () to obtain the date directly, no need to get the year, month, day alone

and tolocaletimestring () can be directly obtained when seconds. Because the format it gets is not required. can then be obtained separately.



JS Gets the current date time and refreshes periodically

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.