手机
当前位置:查字典教程网 >编程开发 >AJAX相关 >AJAX获取服务器当前时间及时间格式输出处理
AJAX获取服务器当前时间及时间格式输出处理
摘要:AJAX获取服务器当前时间------------------------------WebService1.asmx-----------...

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>

【AJAX获取服务器当前时间及时间格式输出处理】相关文章:

Ajax上传实现根据服务器端返回数据进行js处理的方法

Ajax获取XMLHttp对象的方法

JQuery中Ajax的Post提交在IE下中文乱码的解决方法

AJAX天气预报前台

基于Ajax表单提交及后台处理简单的应用

浅析JSONP之解决ajax跨域问题

AJax实现类似百度搜索栏的功能 (面试多见)

AJAX下的请求方式以及同步异步的区别小结

ajax处理服务器返回的三种数据类型方法

Ajax Hacks chm文件与源代码 下载

精品推荐
分类导航