AJAX获取服务器当前时间及时间格式输出处理

AJAX获取服务器当前时间

  ------------------------------ WebService1.asmx----------------------------------

  

复制代码 代码如下:

  // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。

  [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---------------------------------------

  

复制代码 代码如下:

  <html xmlns="http://www.w3.org/1999/xhtml">

  <head>

  <title></title>

  <script src="js/Jquery1.7.js" type="text/javascript"></script>

  <script type="text/javascript">

  $(function () {

  function GetDate() {

  $.ajax({

  type: "post", //客户端向服务器发送请求时采取的方式

  contentType: "application/json", //指定客户端发送给服务器的内容的类型以及服务器返回给客户端内容的类型为json格式

  url: "WebService1.asmx/GetDate", //指明客户端要向哪个页面里面的哪个方法发送请求

  data: "{}", //指定伴随发送的请求传递到服务器的参数

  success: function (result) {//客户端调用服务器端方法成功后执行的回调函数。

  $('#mydiv').text(result.d);

  }

  })

  }

  setInterval(GetDate, 1000);

  })

  </script>

  </head>

  <body>

  <div id="mydiv"></div>

  <input id="Button1" type="button" value="获取服务器时间" />

  </body>

  </html>

  ajax 获取服务器时间

  

复制代码 代码如下:

  <script language="javascript" type="text/javascript">

  //因程序执行耗费时间,所以时间并不十分准确,误差大约在2000毫秒以下

  var xmlHttp = false;

  //获取服务器时间

  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"));

  //获取服务器日期

  var year=severtime.getFullYear();

  var month=severtime.getMonth()+1;

  var date=severtime.getDate();

  //获取服务器时间

  var hour=severtime.getHours();

  var minu=severtime.getMinutes();

  var seco=severtime.getSeconds();

  //格式化输出服务器时间

  function getSeverTime(){

  seco++;

  if(seco==60){

  minu+=1;

  seco=0;

  }

  if(minu==60){

  hour+=1;

  minu=0;

  }

  if(hour==24){

  date+=1;

  hour=0;

  }

  //日期处理

  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){//闰年处理

  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="当前服务器时间:";

  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>

  

复制代码 代码如下:

  <body onLoad="getSeverTime();">

  <p id="servertime"></p>

  <p id="clienttime"></p>

  <p id="xctime"></p>

  </body>