Js-implemented time and date code instances that can be automatically refreshed, and js-automatically refreshed

Source: Internet
Author: User

Js-implemented time and date code instances that can be automatically refreshed, and js-automatically refreshed

Example of the time and date code implemented by js that can be automatically refreshed:
The time and date effects that can be automatically refreshed are used on many web pages, because they make the pages more dynamic and user-friendly, and users can view the page time in real time, the following describes how to achieve this through code examples.
The Code is as follows:

<! DOCTYPE html> 

The above code implements automatic refresh of time and date. The following describes the implementation process.
I. Code comments:
1. function getTime () {}, which obtains the current time and date.
2. var odate = new Date (), create the object of the current time and Date.
3. var date = odate. toLocaleDateString (), you can obtain the local year, month, and day.
4. var hours = odate. getHours (), get the hour.
5. var minutes = odate. getMinutes (), get minutes.
6. var seconds = odate. getSeconds (), get seconds.
7. $ ("# showDate" 2.16.html (date + "" + (hours> 9? Hours: "0" + hours) + ":" + (minutes> 9? Minutes: "0" + minutes) + ":" + (seconds> 9? Seconds: "0" + seconds) to write the current time and date to the specified element. Of course, this is div, this is to use the ternary operator to add 0 to the front when the hour, minute, and second are single digits.
8. $ (function () {setInterval ("getTime ();", 1000) ;}), the specified function is executed every second.
Ii. Related reading:
1. For more information about time objects, see the Date object section of ECMAScript.
2. For more information about ternary operators, see section 1.
3. For details about setInterval () functions, refer to setInterval () function usage.

The original address is: http://www.softwhy.com/forum.php? Mod = viewthread & tid = 14636.

For more information, see: http://www.softwhy.com/jquery/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.