Ajax get server current time and time format output processing _ajax related

Source: Internet
Author: User
Tags getdate
Ajax Get Server current time
------------------------------Webservice1.asmx----------------------------------
Copy Code code as follows:

To allow the use of ASP.net AJAX to invoke this Web service from a script, uncomment the downlink.
[System.Web.Script.Services.ScriptService]
public class WebService1:System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld ()
{
Return to "Hello World";
}
[WebMethod]
public string GetDate ()
{
Return DateTime.Now.ToString ("Yyyy-mm-dd hh:mm:ss");
}
}

------------------------------------htmlpage1.htm---------------------------------------
Copy Code code as follows:

<title></title>
<script src= "Js/jquery1.7.js" type= "Text/javascript" ></script>
<script type= "Text/javascript" >
$ (function () {
function GetDate () {
$.ajax ({
Type: "POST",//How the client takes the request when sending it to the server
ContentType: "Application/json",//Specifies the type of content that the client sends to the server and the type that the server returns to the client content in JSON format
URL: "Webservice1.asmx/getdate",//indicates to which page the client is going to send the request
Data: "{}",//Specify parameters to be passed to the server with the sent request
Success:function (Result) {////The callback function executed after the client invokes the server-side method successfully.
$ (' #mydiv '). Text (RESULT.D);
}
})
}
SetInterval (GetDate, 1000);
})
</script>
<body>
<div id= "Mydiv" ></div>
<input id= "Button1" type= "button" value= "Get server Time"/>
</body>

Ajax Get server time
Copy Code code as follows:

<script language= "javascript" type= "Text/javascript" >
The time is not very accurate due to the execution of the program, the error is less than 2000 milliseconds
var xmlHttp = false;
Get 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"));
Get Server Date
var year=severtime.getfullyear ();
var month=severtime.getmonth () +1;
var date=severtime.getdate ();
Get server time
var hour=severtime.gethours ();
var minu=severtime.getminutes ();
var seco=severtime.getseconds ();
Format 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&&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 Code code 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.