Home > Web Front-end > JS Tutorial > body text

JS implementation of code sharing to obtain the current date and refresh it regularly

韦小宝
Release: 2018-03-14 18:35:52
Original
1548 people have browsed it

This article mainly shares with you the JS code to obtain the current date and refresh it regularly. First, we will share with you the JS code to obtain the current date and time. I hope it can help everyone.

var date = new Date();
date.getYear();        //获取当前年份(2位)
date.getFullYear();    //获取完整的年份(4位,2014)
date.getMonth();       //获取当前月份(0-11,0代表1月)
date.getDate();        //获取当前日(1-31)
date.getDay();         //获取当前星期X(0-6,0代表星期天)
date.getTime();        //获取当前时间(从1970.1.1開始的毫秒数)
date.getHours();       //获取当前小时数(0-23)
date.getMinutes();     //获取当前分钟数(0-59)
date.getSeconds();     //获取当前秒数(0-59)
date.getMilliseconds();    //获取当前毫秒数(0-999)
date.toLocaleDateString();     //获取当前日期   如 2014年6月25日 
date.toLocaleTimeString();     //获取当前时间   如 下午4:45:06
date.toLocaleString();         //获取日期与时间 如 2014年6月25日 下午4:45:06
Copy after login

Note: Both getYear() and getFullYear() can get the year, but there is a slight difference between the two.

The getYear() displayed in the browser is: 114 (2014 is used as the year) Example), the reason is that getYear returns the value of "current year-1900" (that is, the base of the year is 1900)

Use JS to get the year: getFullYear()

Refresh regularly
Use setInterval for scheduled refresh. For details on the difference between setTimeout and setInterval, please refer to other information.

1. First, the page needs an area to display the time.

<p id="showDate"></p>
Copy after login


2. Get the time.

<script type="text/javascript">
$(function(){
setInterval("getTime();",1000); //每隔一秒运行一次
})
//取得系统当前时间
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); //将值赋给p
}
</script>
Copy after login

Use toLocaleDateString() to directly obtain the year, month and day. No need. Then separately obtain the year, month, and day

, and toLocaleTimeString() can directly obtain the hours, minutes, and seconds. Because the format it gets is not needed. So it can be obtained separately.

Related recommendations:

Js methods to obtain the current date and time and other operations

The above is the detailed content of JS implementation of code sharing to obtain the current date and refresh it regularly. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template