如何获取远程WEB服务器的时间

如何获取远程WEB服务器的时间,第1张

 获取远程WEB服务器时间可以利用服务器返回的头信息获取,使用JS代码方法如下,其它语言同理:

<script>

var xmlhttp=new ActiveXObject("MSXML2.XMLHTTP.3.0")

xmlhttp.open("GET","远程服务器地址",false)

xmlhttp.setRequestHeader("If-Modified-Since","q")

xmlhttp.send()

var dateStr= xmlhttp.getResponseHeader("Date")

//alert(dateStr)

var d = new Date(dateStr)

document.write(d)

</script>

Date date = new Date()

String str = SimpleDateFormat.format(date)

System.out.println(str)

昨天刚好做了一个,把下面的内容保存为ASPX文件就可以了:

<form runat=server>

下面这个Hidden变量用来暂存服务器时间

<input name="ServerTime" type="hidden" value="<%=DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")%>">

<input name="divCurrentUser" type="text" style="width:300pxborder:0" readonly>

</form>

<script language=javascript>

getServerTime()

//计算当前服务器时间

function getServerTime()

{

//取得要进行显示的日期

var datetimeYou = document.getElementById('ServerTime').value.split(" ")

var yout1 = datetimeYou[0].split("-")

var yout2 = datetimeYou[1].split(":")

var nowY = parseFloat(yout1[0])

var nowM = parseFloat(yout1[1])

var nowD = parseFloat(yout1[2])

var h = parseFloat(yout2[0])

var m = parseFloat(yout2[1])

var s = parseFloat(yout2[2])

var daysMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)

if(leapyear(nowY))

daysMonth[1] += 1//闰年,2月加1

s = s + 1

if (s >= 60)

{

s = 0

m = m + 1

if (m >= 60)

{

m = 0

h = h + 1

if (h >= 24)

{

h = 0

nowD += 1

if(nowD >daysMonth[nowM-1])

{

nowD = 1

nowM += 1

if(nowM >12)

{

nowM = 1

nowY += 1

}

}

}

}

}

if (nowM <10) nowM = "0" + nowM

if (nowD <10) nowD = "0" + nowD

if (h <10) h = "0" + h

if (m <10) m = "0" + m

if (s <10) s = "0" + s

document.getElementById('ServerTime').value = nowY + "-" + nowM + "-" + nowD + " " + h + ":" + m + ":" + s

var today,hour,second,minute,year,month,date

var strDate

//注意:Javascript中的月要减1,比如现在4月,要写成3

today=new Date(yout1[0],yout1[1]-1,yout1[2],yout2[0],yout2[1],yout2[2])

var n_day = today.getDay()

switch (n_day)

{

case 0:{

strDate = "星期日"

}break

case 1:{

strDate = "星期一"

}break

case 2:{

strDate ="星期二"

}break

case 3:{

strDate = "星期三"

}break

case 4:{

strDate = "星期四"

}break

case 5:{

strDate = "星期五"

}break

case 6:{

strDate = "星期六"

}break

case 7:{

strDate = "星期日"

}break

}

year = today.getFullYear()

month = today.getMonth() + 1

date = today.getDate()

hour = today.getHours()

minute =today.getMinutes()

second = today.getSeconds()

if(month<10) month="0"+month

if(date<10) date="0"+date

if(hour<10) hour="0"+hour

if(minute<10) minute="0"+minute

if(second<10) second="0"+second

document.getElementById('divCurrentUser').value = "当前时间:" + year + "年" + month + "月" + date + "日 " + strDate +" " + hour + ":" + minute + ":" + second//显示时间

setTimeout("getServerTime()",1000)

}

//判断参数是否闰年

function leapyear(year){

if(!/[^0]\d+/.test(year)){

//通过正则判断年份的合法性

return false

}

if(0==year)

return true

if ( ((0==year%4) &&!(0==year%100)) || (0==year%400) ){

return true

}else{

if(year!=0)

return false

}

}

</script>


欢迎分享,转载请注明来源:夏雨云

原文地址:https://www.xiayuyun.com/zonghe/120934.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-03-14
下一篇2023-03-14

发表评论

登录后才能评论

评论列表(0条)

    保存