JS get the current date and time and regularly refresh example _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces how JS obtains the current date and time and performs timed refresh. The sample code is as follows. Do not miss it if you need it. JS get current date and time

Var date = new Date (); date. getYear (); // obtain the current year (2 digits) date. getFullYear (); // obtain the complete year (4 bits, 2014) date. getMonth (); // obtain the current month (0-11, 0 represents January) date. getDate (); // get the current day (1-31) date. getDay (); // obtain the date of the current week X (0-6, 0 represents Sunday. getTime (); // get the current time (milliseconds starting from 1970.1.1) date. getHours (); // obtain the current hour (0-23) date. getMinutes (); // get the current number of minutes (0-59) date. getSeconds (); // obtain the current number of seconds (0-59) date. getMilliseconds (); // get the current number of milliseconds (0-999) date. toLocaleDateString (); // obtain the current date, for example, January 1, June 25, 2014 date. toLocaleTimeString (); // obtain the current time, such as 4:45:06 date. toLocaleString (); // obtain the date and time, such as June 25, 2014 4:45:06

Note: Both getYear () and getFullYear () can obtain the year, but the two are slightly different.

GetYear () is displayed in the browser as 114 (for example, January 1, 2014), because getYear returns the value of "current year-1900" (that is, the year base is 1900)

Use JavaScript to retrieve the year: getFullYear ()

Timed refresh

SetInterval is used for timed refresh. For details about the difference between setTimeout and setInterval, refer to other materials.

1. First, the page requires a region for displaying the time

2. Obtain the time

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.