The following uses asp + js as an example. The specific code can be downloaded from the website time code.
The following code structure is analyzed to obtain the server time:
Copy codeThe Code is 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) %>;
// Obtain the difference between local time and server time
Var dectime = new Date (nyear, nmonth-1, nday, nhrs, nmin, nsec). getTime ()-t0;
Nday =-1;
// Span used to display the 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 + = "" + lmonth + "" + lday; // display the lunar calendar
Document. getElementById ("clock"). innerHTML = s;
// Obtain the local time every 500 milliseconds:
SetTimeout ("Draw ()", 500 );
}
// Start to display:
Draw ();
Other questions:
The time obtained by the server cannot be cached locally. Therefore, you must add a cache control function to the code;
To prevent other websites from leeching your server time code, you must add anti-leech functions.
To display the lunar calendar, you also need to add the lunar calculation function.
For more information, download the code.