AJAX obtains the current time and time format output of the server.

Source: Internet
Author: User

AJAX obtains the current server time
------------------------------ WebService1.asmx ---------------------------------- Copy codeThe Code is as follows: // to allow ASP. net ajax to call this Web service from a script, cancel the comments to the downstream.
[System. Web. Script. Services. ScriptService]
Public class WebService1: System. Web. Services. WebService
{
[WebMethod]
Public string HelloWorld ()
{
Return "Hello World ";
}
[WebMethod]
Public string GetDate ()
{
Return DateTime. Now. ToString ("yyyy-MM-dd hh: mm: ss ");
}
}

------------------------------------HTMLPage1.htm ---------------------------------------Copy codeThe Code is as follows: <Head>
<Title> </title>
<Script src = "js/Jquery1.7.js" type = "text/javascript"> </script>
<Script type = "text/javascript">
$ (Function (){
Function GetDate (){
$. Ajax ({
Type: "post", // method used when the client sends a request to the server
ContentType: "application/json", // specifies the type of content that the client sends to the server and the type of content that the server returns to the client in json format.
Url: "WebService1.asmx/GetDate", // specifies the method on which the client sends the request
Data: "{}", // specify the parameters that are sent along with the request to the server
Success: function (result) {// callback function executed after the client successfully calls the server method.
$ ('# Mydiv'). text (result. d );
}
})
}
SetInterval (GetDate, 1000 );
})
</Script>
</Head>
<Body>
<Div id = "mydiv"> </div>
<Input id = "Button1" type = "button" value = "Get server time"/>
</Body>
</Html>

Ajax obtains the server time Copy codeThe Code is as follows: <script language = "javascript" type = "text/javascript">
// Because the execution of the program takes time, the time is not very accurate, with an error of less than 2000 milliseconds
Var xmlHttp = false;
// Obtain the server time
Try {
XmlHttp = new ActiveXObject ("Msxml2.XMLHTTP ");
} Catch (e ){
Try {
XmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ");
} Catch (e2 ){
XmlHttp = false;
}
}
If (! XmlHttp & typeof XMLHttpRequest! = 'Undefined '){
XmlHttp = new XMLHttpRequest ();
}
XmlHttp. open ("GET", "http://www.time.ac.cn", false );
XmlHttp. setRequestHeader ("Range", "bytes =-1 ");
XmlHttp. send (null );
Severtime = new Date (xmlHttp. getResponseHeader ("Date "));
// Obtain the server date
Var year = severtime. getFullYear ();
Var month = severtime. getMonth () + 1;
Var date = severtime. getDate ();
// Obtain the server time
Var hour = severtime. getHours ();
Var minu = severtime. getMinutes ();
Var seco = severtime. getSeconds ();
// Format the output server time
Function getSeverTime (){
Seco ++;
If (seco = 60 ){
Minu + = 1;
Seco = 0;
}
If (minu = 60 ){
Hour + = 1;
Minu = 0;
}
If (hour = 24 ){
Date + = 1;
Hour = 0;
}
// Date Processing
If (month = 1 | month = 3 | month = 5 | month = 7
| Month = 8 | month = 10 | month = 12)
{
If (date = 32)
{
Date = 1;
Month + = 1;
}
} Else if (month = 4 | month = 6 | month = 9 | month = 11 ){
If (date = 31 ){
Date = 1;
Month + = 1;
}
} Else if (month = 2 ){
If (year % 4 = 0 & amp; year % 100! = 0) {// leap year Processing
If (date = 29 ){
Date = 1;
Month + = 1;
}
} Else {
If (date = 28 ){
Date = 1;
Month + = 1;
}
}
}
If (month = 13 ){
Year + = 1;
Month = 1;
}
Sseco = addZero (seco );
Sminu = addZero (minu );
Shour = addZero (hour );
Sdate = addZero (date );
Smonth = addZero (month );
Syear = year;
Innerdata = "current server time :";
Document. getElementById ("servertime "). innerHTML = innerdata + syear + "-" + smonth + "-" + sdate + "" + shour + ":" + sminu + ":" + sseco;
SetTimeout ("getSeverTime ()", 1000 );
SetTimeout ("getClientTime ()", 100 );
}
Function addZero (num ){
Num = Math. floor (num );
Return (num <= 9 )? ("0" + num): num );
}
</Script>

Copy codeThe Code is as follows: <body onLoad = "getSeverTime ();">
<P id = "servertime"> </p>
<P id = "clienttime"> </p>
<P id = "xctime"> </p>
</Body>

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.