In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article "javascript how to get the current time", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "javascript how to get the current time" article.
In javascript, you can use the Date () method in the Date object to get the current time, which returns the date and time of the day, or you can use "new Date ()" to create a time object that uses the built-in time reading method to get information such as year, month, day, hour, minute, second, and so on.
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
In javascript, you can use the Date () method in the Date object to get the current time. The Date () method returns the date and time of the day.
Date ()
Example: output today's date and time
Console.log (Date ())
Output:
Or you can use different methods in the Date object to format the time and display it in a fixed format.
First, use new Date () to create a present time object, and then use get as the prefix time reading method to obtain the present tense information such as year, month, day, hour, minute, second and so on. Finally, the timer is set to execute once per second to achieve real-time update.
[procedure]
1) Design the time display function, in which the Date object is created to obtain the current time, and then the year, month, day, hour, minute, second and other information are obtained respectively, and finally assembled into a time string and returned.
Var showtime = function () {var nowdate = new Date () Var year = nowdate.getFullYear (), month = nowdate.getMonth () + 1, date = nowdate.getDate (), day = nowdate.getDay (), week = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"], h = nowdate.getHours (), m = nowdate.getMinutes () S = nowdate.getSeconds (), h = checkTime (h), m = checkTime (m), s = checkTime (s) Return year + year + month + month + date + Day + week [day] + "+ h +": "+ m +": "+ s;}
2) because the usual time format is 00:00:01, and the getHours (), getMinutes (), getSeconds () methods get a format of 0 to 9 instead of 00 to 09. So in the process from 9 to 10, from one digit to two digits, again from 59 seconds to zero seconds, or 59 minutes to zero minutes, or 23:00 to zero. For example: 23:59:59 the next second should be 00:00:00, actually 0:0:0, so that the format is not uniform, but also a sudden increase in visual number, or sudden decrease, resulting in a sense of shaking.
An auxiliary function is defined below to change the time of one digit to two digits.
Var checkTime = function (I) {if (I < 10) {I = "0" + I;} return I;}
3) add a tag to the page and set the value of id.
4) bind the timer for the label and set the time display function once per second clock in the timer.
Var div = document.getElementById ("showtime"); setInterval (function () {div [XSS _ clean] = showtime ();}, 1000); / / execute the function repeatedly
The display results are as follows:
The above is about the content of this article on "how to obtain the current time of javascript". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.