JS Simple Method of Obtaining and Displaying Current Time

  • 2021-07-07 06:24:02
  • OfStack

In this paper, an example is given to describe the simple method of obtaining and displaying the current time in JS. Share it for your reference, as follows:


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title> Current time </title>
</head>
<body>
 <script>
var now = new Date();
var year = now.getFullYear();    // Year 
var month = now.getMonth() + 1;   // Month 
var day = now.getDate();      // Day 
var hh = now.getHours();      // Hour 
var mm = now.getMinutes();     // Points 
var clock = year + "-";
if (month < 10)
clock += "0";
clock += month + "-";
if (day < 10)
clock += "0";
clock += day + " ";
if (hh < 10)
clock += "0";
clock += hh + ":";
if (mm < 10) clock += '0';
clock += mm;
document.write(clock);
</script>
</body>
</html>

PS: Friends who are interested in JavaScript time and date operation can also refer to this online tool:

Unix timestamp (timestamp) conversion tool:
http://tools.ofstack.com/code/unixtime

Online time query around the world:
http://tools.ofstack.com/zhuanhuanqi/worldtime

Online perpetual calendar:
http://tools.ofstack.com/bianmin/wannianli

Page perpetual calendar calendar:
http://tools.ofstack.com/bianmin/webwannianli

For more information about JavaScript, please see the topics of this site: "Summary of JavaScript Time and Date Operation Skills", "Summary of JavaScript Switching Special Effects and Skills", "Summary of JavaScript Search Algorithm Skills", "Summary of JavaScript Animation Special Effects and Skills", "Summary of JavaScript Error and Debugging Skills", "Summary of JavaScript Data Structure and Algorithm Skills", "Summary of JavaScript Traversal Algorithm and Skills" and "Summary of JavaScript Mathematical Operation Usage"

I hope this article is helpful to everyone's JavaScript programming.


Related articles: