JS Real-time acquisition of the system current Times instance code

Source: Internet
Author: User
Tags current time
Displays the time in the Web page in real-time, not only can give the Web page Tim, but also can facilitate the viewer to grasp the current time, in order to improve the speed of web site development, can be encapsulated in a separate function of the main code, in need of direct call and I for the demonstration, directly written in the main page, to facilitate everyone to watch

(1): First with JS to write real-time display system time function Clockon () only a parameter bgclock, used to specify the display for the conversion of the name of the <div> tag, no return value, you can change the function of Web site development in the JS file, in order to reuse The code is as follows:

Copy Code code as follows:


&lt;script&gt;


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&lt;10) month= "0" +month;


if (date&lt;10) date= "0" +date;


if (hour&lt;10) hour= "0" +hour;


if (minu&lt;10) minu= "0" +minu;


if (sec&lt;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);


}


&lt;/script&gt;

(2): Call in the <body> onload event of the page and place the DIV tag code where you want to display it:

Copy Code code as follows:


<body onload= "Clockon (bgclock)" >
<div id= "Bgclock" class= "Word_green" ></div>
</body>

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.