How to display server time in Web pages-related tips

Source: Internet
Author: User

The following is an example of ASP+JS. Specific code can be downloaded to the website time Code of this website.
The following is an analysis of the code structure, first getting the server time:

Copy Code code as follows:

<%
D=date ()
T=time ()
%>
var t0=new Date (). GetTime ();
var nyear=<%=year (d)%>;
var nmonth=<%=month (d)%>;
var nday=<%=day (d)%>;
var nwday=<%=weekday (d)%>-1;
var nhrs=<%=hour (t)%>;
var nmin=<%=minute (t)%>;
var nsec=<%=second (t)%>;
Get the difference between native time and server time
var dectime=new Date (nyear,nmonth-1,nday,nhrs,nmin,nsec). GetTime ()-t0;
Nday=-1;
span for displaying time
document.write ("<span id=\" clock\ "style=\" word-break:keep-all\ "></span>");
Time calculation function:
function Newtick () {
var noww=new Date ();
var t1=noww.gettime ();
Noww.settime (T1+dectime);
if (Noww.getdate ()!=nday) {
Nyear=noww.getfullyear (); Nmonth=noww.getmonth () +1;nwday=noww.getday (); Nday=noww.getdate ();
}
Nhrs=noww.gethours (); Nmin=noww.getminutes (); Nsec=noww.getseconds ();
}
Time display function:
function Draw () {
Newtick (); Calculate first
var s=nyear+ ' year ' +nmonth+ ' month ' +nday+ ' Day ' + ' Week ' +cweekday (nwday) + ' +shapetime (nhrs,nmin,nsec);
s+= "lunar" +lmonth+ "month" +lday;//display lunar calendar
document.getElementById ("Clock"). Innerhtml=s;
Re-obtain the native time every 500 milliseconds:
SetTimeout ("Draw ()", 500);
}

Start showing:
Draw ();



Other questions:

The time the server gets cannot be cached locally, so add the cache control function to the code.
Also to prevent other sites hotlinking your server time code, so to join the anti-theft chain function.
If you need to display the lunar calendar, you will also need to add a lunar operation function.
Please download the code to see for yourself.

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.