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