JS instance code _ javascript tips for real-time Page Update Time

Source: Internet
Author: User
This article mainly introduces the JS instance code for real-time page update. If you need it, refer to it. The Code is as follows:


Function startTime (){
Var today = new Date (); // defines the Date object
Var yyyy = today. getFullYear (); // returns the year by using the getFullYear () method of the date object.
Var MM = today. getMonth () + 1; // returns the year using the getMonth () method of the date object.
Var dd = today. getDate (); // returns the year using the getDate () method of the date object.
Var hh = today. getHours (); // returns the hour through the getHours method of the date object
Var mm = today. getMinutes (); // returns the minute through the getMinutes method of the date object
Var ss = today. getSeconds (); // returns the second through the getSeconds method of the date object
// If the minute or hour value is less than 10, add 0 before the value. For example, if the time is 03:20:09 P.M., 15:20:09 is displayed.
MM = checkTime (MM );
Dd = checkTime (dd );
Mm = checkTime (mm );
Ss = checkTime (ss );
Var day; // used to save the week (getDay () method to get the day of the week)
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 '). innerHTML = yyyy + "-" + MM + "-" + dd + "" + hh + ":" + mm + ":" + ss + "" + day;
SetTimeout ('starttime () ', 1000); // reload the startTime () method every second.
}

Function checkTime (I ){
If (I <10 ){
I = "0" + I;
}
Return I;
}

Call:Current Time:

Related Article

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.