How to display server time in a Web page (ASP)

Source: Internet
Author: User
Tags getdate

display the time on the webpage, if take is the user this machine time, because the user's time is often inaccurate, therefore displays the question. And the server time generally will not be too large error, so it is best to display the server time

The following is an example of ASP+JS.

The following is an analysis of the code structure, first getting the server time:

<%
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
document.write ("<span id=" Clock "style=" Word-break:keep-all "></span>") for displaying time;
//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); The
s+= "lunar" +lmonth+ "month" +lday;//displays the 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.

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.