编程知识 cdmana.com

JavaScript - get current time and display (using timer)

This feature is available on the web as well as APP It's very common in . So share it here .

The code is as follows :

<p>
     The current time is :<span class='time'></span>
</p>

<script>
setInterval(function(){
    var date = new Date();// Get the timestamp form 
    document.querySelector('.time').innerHTML = `
        ${date.getFullYear()} year ${date.getMonth()+1} month ${date.getDate()} Japan -${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`
},1000)
</script>

Code involved in the small knowledge points :

1、document.quertSelector(); by html5 New acquisition DOM The way of elements 
2、 How to get the current time :new Date();
3、`` For template string , The writing method is more concise than the traditional string ,`${}` that will do 
4、 The timer used in the current code can be executed multiple times 
5、 Get the month , yes 0-11, So in use , To do it +1

版权声明
本文为[Wang Chao]所创,转载请带上原文链接,感谢

Scroll to Top