Get the App
SLTechnology News&Howtos  ›  Development  › 

How to display time dynamically by javascript

Shulou Source: shulou.com Published: 2022-06-03 12:30:42 09月29日 Update

This article mainly shows you "javascript how to dynamically display time", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "javascript how to dynamically display time" this article.

Javascript display time method: 1, use "new Date ()" to define the date object; 2, use the date object's getFullYear (), getMonth () and other methods to obtain the year, month, day, hour and second value and output; 3, use the setTimeout () method to refresh the time value once a second.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Javascript dynamic display time implementation code:

Dynamically display system date time function startTime () {var today=new Date (); / / define date object var yyyy = today.getFullYear (); / / return year var MM = today.getMonth () + 1 through the date object's getFullYear () method / / return the year var dd = today.getDate () through the date object's getMonth () method; / / return the year var hh=today.getHours () through the date object's getDate () method; / / return the hour var mm=today.getMinutes () through the date object's getHours method / / return minutes var ss=today.getSeconds () through the date object's getMinutes method; / / return seconds through the date object's getSeconds method / / if the value of the minute or hour is less than 10, add 0 to its value, for example, if the time is 03:20:09 in the afternoon, it shows 15:20:09 MM=checkTime (MM) Dd=checkTime (dd); mm=checkTime (mm); ss=checkTime (ss); var day / used to save the week (getDay () method gets the week number) if (today.getDay () = = 0) day = "Sunday" if (today.getDay () = = 1) day = "Monday" if (today.getDay () = = 2) day = "Tuesday" if (today.getDay ( ) = 3) day = "Wednesday" if (today.getDay () = = 4) day = "Thursday" if (today.getDay () = = 5) day = "Friday" if (today.getDay () = = 6) day = "Saturday" document.getElementById ('nowDateTimeSpan') [xss_clean] = yyyy+ "-" + MM + "-" + dd + "" + hh+ ":" + mm+ ":" + ss+ "" + day SetTimeout ('startTime ()', 1000); / / reload the startTime () method} function checkTime (I) {if (I) every second

Tags: Time format date object method week hour year dynamic hour number month content article system entity player standard abbreviation learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia macOS Linux Shulou Technology vpn