当前位置: 移动技术网 > IT编程>脚本编程>Ajax > AJAX获取服务器当前时间及时间格式输出处理

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

2017年12月12日  | 移动技术网IT编程  | 我要评论
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>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网