Js real-time acquisition of system current time instance code _ javascript tips-js tutorial

Source: Internet
Author: User
The real-time display time on the webpage not only adds color to the webpage, but also helps the viewer master the current time. In order to improve the website development speed, you can encapsulate the main code in a separate function and call it directly when necessary. For demonstration, I wrote it on the home page for your convenience (1 ): first, write the function clockon () for real-time system time display in JS with only one parameter bgclock, which is used to specify the display for the converted

The marked name has no returned value. When you develop a website, you can save the modified function in the JS file to reuse the Code as follows:

The Code is as follows:


Script
Function clockon (bgclock)
{
Var now = new Date ();
Var year = now. getYear ();
Var month = now. getMonth ();
Var date = now. getDate ();
Var day = now. getDay ();
Var hour = now. getHours ();
Var minu = now. getMinutes ();
Var sec = now. getSeconds ();
Var week;
Month = month + 1;
If (month <10) month = "0" + month;
If (date <10) date = "0" + date;
If (hour <10) hour = "0" + hour;
If (minu <10) minu = "0" + minu;
If (sec <10) sec = "0" + sec;
Var arr_week = new Array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday ");
Week = arr_week [day];
Var time = "";
Time = year + "year" + month + "month" + date + "day" + week + "" + hour + ";" + minu + ";" + sec;
If (document. all)
{
Bgclock. innerHTML = "[" + time + "]"
}
Var timer = setTimeout ("clockon (bgclock)", 200 );
}
Script

(2 ):Called in the onload event and put the p tag code in the place to display:

The Code is as follows:




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.