According to the server time as the starting, show the Clock applet _javascript tips

Source: Internet
Author: User

<span id= "Curtime" ></span> <script type= "Text/javascript" ><!--setinterval ("curtime.innerhtml =new date (). toLocaleString () + ' Week ' + ' Day 123456 '. CharAt (New Date (). Getday ()); ", 1000); --></script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

I am in the OA of an attendance program, to dynamically display the time of the server, so that employees know whether the current can work, and the client for various reasons time and server is inconsistent, so can not use the above code, improved a bit:
<span id= "Curtime" ></span> <script type= "Text/javascript" ><!--//page to start timing date (typically the server date), Like 2007-01-12 11:34:55. var beginadd = new Date (2007,0,12,11,34,55); var begindate = new Date (); function ShowTime () {var now = new Date (); var diff = (now-begindate); Beginadd.setmilliseconds (Beginadd.getmilliseconds () + diff); Begindate = Now; document.getElementById ("Curtime"). InnerHTML = beginadd.tolocalestring () + ' Week ' + ' Day 123456 '. CharAt (Beginadd.getday () ); } setinterval ("ShowTime ();", 1000); --></script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Finally, add a meizz Format function (slightly modified), the final version is as follows:
<span id= "Curtime" ></span> <script type= "Text/javascript" ><!--//page to start timing date (typically the server date), Like 2007-01-12 11:34:55. var beginadd = new Date (2007,0,12,11,34,55); var begindate = new Date (); Function abc () {var now = new Date (); var diff = (now-begindate); Beginadd.setmilliseconds (Beginadd.getmilliseconds () + diff); Begindate = Now; document.getElementById ("Curtime"). InnerHTML = Beginadd.format ("YYYY year mm month DD Day HH:MM:SS week W"); } setinterval ("ABC ();", 1000); Date.prototype.format = function (format) {var obj = {"m+": This.getmonth () + 1,//returns the actual month "d+": t His.getdate (),//Returns the day of the Month "h+": this.gethours (),//Return Hour "m+": this.getminutes (),//Return minute "s+": this.getsec Onds (),//return second "q+": Math.floor (This.getmonth () + 3)/3),//Back to Quarter "S": This.getmilliseconds (),//back to milliseconds "W": This.getday (),//Return of the week, 0 for Sunday "W": "Day 123456". CharAt (This.getday ())//Return of the days of the week in Chinese Representation}//yearSeparate processing if (/(y+)/.test (format)) format = Format.replace (regexp.$1, this.getfullyear () + ""). substr (4-regexp. $1.length)); Other format processing for (var k in obj) {if (New RegExp ("+ K +")). Test (format) format = Format.replace (RegEx p.$1, regexp.$1.length = 1? Obj[k]: ("+ obj[k]"). substr (("" "+ obj[k]). length); } return format; }//--></script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

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.