js取服务器时间显示时钟(js获取实时时间)
- 服务器
- 2022-12-19 02:40:30
- 16
今天给各位分享js取服务器时间显示时钟的知识,其中也会对js获取实时时间进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
JS如何获取当前系统时间
系统时间一般是值服务端时间,js获取服务端时间的方法是直接用ajax获取。
1、编写显示时间的页面:
html
head
meta http-equiv="Content-Type" content="text/html; charset=utf-8" /
titleServer date/time/title
script language="javascript" src="serverDate.js"/script
/head
script language="javascript"
var localTime = new Date();
document.write("Local machine time is: " + localTime + "br");
document.write("Server time is: " + date);
/script
body
/body
2、ajax脚本获取server的时间
var xmlHttp;
function srvTime(){
try {
//创建xmlHttp对象
xmlHttp = new XMLHttpRequest();
}
catch (err1) {
//ie浏览器
try {
xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
}
catch (err2) {
try {
xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
}
catch (eerr3) {
//ajax不支持
alert("AJAX not supported");
}
}
}
//打开xmlHttp请求
xmlHttp.open('HEAD',window.location.href.toString(),false);
//设置xmlHttp请求头
xmlHttp.setRequestHeader("Content-Type", "text/html");
//发送请求
xmlHttp.send('');
// 获取response中的Date参数
return xmlHttp.getResponseHeader("Date");
}
var st = srvTime(); //服务器时间赋值给st变量
var date = new Date(st); //转换js的date对象
// 输出服务器时间
document.write("服务器时间: " + date);
js如何获取服务器时间
直接给你代码吧,这个是JS可以读取本地时间和服务器时间的。如果想看效果可以看参考资料
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""
html xmlns="" xml:lang="en" lang="en"
head
meta http-equiv="Content-Type" content="text/html; charset=utf-8"/
titleuntitled/title
script type="text/javascript"
get=function (id){return document.getElementById(id)}
if(document.all){
window.XMLHttpRequest=function(){
var get=['Microsoft.XMLHTTP','Msxml2.XMLHTTP'];
for(var i=0;iget.length;i++){try{return new ActiveXObject(get[i])}catch(e){}};
};
}
webDate=function(fn){
var Htime=new XMLHttpRequest();
Htime.onreadystatechange=function(){Htime.readyState==4(fn(new Date(Htime.getResponseHeader('Date'))))};
Htime.open('HEAD', '/?_='+(-new Date));
Htime.send(null);
}
window.time=new Date();
targetTime=new Date();
time2String=function (t){
with(t)return [getFullYear(),'年'
,('0'+(getMonth()+1)).slice(-2),'月'
,('0'+getDate()).slice(-2),'日 '
,('0'+getHours()).slice(-2),': '
,('0'+getMinutes()).slice(-2),': '
,('0'+getSeconds()).slice(-2)].join('')
}
int2time=function (m){
m-=(D=parseInt(m/86400000))*86400000;
m-=(H=parseInt(m/3600000))*3600000;
S=parseInt((m-=(M=parseInt(m/60000))*60000)/1000);
return D+'天'+H+'小时'+M+'分'+S+'秒'
}
setInterval(function (){
webDate(function (webTime){
get('web').innerHTML=time2String(time=webTime);
})
get('locale').innerHTML=time2String(new Date);
get('time').innerHTML=int2time(targetTime-time);
if ((targetTime-time)0) {
get('time').innerHTML = 'Game Over';
}
},1000)
/script
/head
body
设定时间:2012年12月31日0时0分0秒br /
服务器时间:span id='web'loading.../spanbr /
本地时间:span id="locale"loading.../spanbr /
倒计时时间:span id="time"loading.../span
script type="text/javascript" charset="utf-8"
targetTime=new Date(2012,12,31,00,00,00);
/script
/body
/html
js 怎么获取年月日时分秒中的时分秒
需要准备的材料分别有:电脑、html编辑器、浏览器。
1、首先,打开html编辑器,新建html文件,例如:index.html。
2、在index.html中的script标签,输入js代码:
var a = new Date();document.body.innerHTML
= '时:' + a.getHours() + 'br/分:' + a.getMinutes() + 'br/秒:' + a.getSeconds();
3、浏览器运行index.html页面,此时当前时间的时分秒都被js获取并打印了出来。
JS倒计时页面代码(要求读取服务器时间)
用JavaScript获取服务器时间,然后做页面倒计时的程序代码如下:
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""
html xmlns="" xml:lang="en" lang="en"
head
meta http-equiv="Content-Type" content="text/html; charset=utf-8"/
titleuntitled/title
script type="text/javascript"
get=function (id){
return document.getElementById(id)
}
if(document.all){
window.XMLHttpRequest=function(){
var get=['Microsoft.XMLHTTP','Msxml2.XMLHTTP'];
for(var i=0;iget.length;i++)
{
try{
return new ActiveXObject(get[i])
}
catch(e){}
};
};
}
webDate=function(fn){
var Htime=new XMLHttpRequest();
Htime.onreadystatechange=function(){
Htime.readyState==4(fn(new Date(Htime.getResponseHeader('Date'))))
};
Htime.open('HEAD', '/?_='+(-new Date)); Htime.send(null);
}
window.time=new Date();
targetTime=new Date();
time2String=function (t){
with(t)return [getFullYear(),'年' ,('0'+(getMonth()+1)).slice(-2),'月' ,('0'+getDate()).slice(-2),'日 ' ,('0'+getHours()).slice(-2),': ' ,('0'+getMinutes()).slice(-2),': ' ,('0'+getSeconds()).slice(-2)].join('')
}
int2time=function (m){
m-=(D=parseInt(m/86400000))*86400000; m-=(H=parseInt(m/3600000))*3600000; S=parseInt((m-=(M=parseInt(m/60000))*60000)/1000);
return D+'天'+H+'小时'+M+'分'+S+'秒'
}
setInterval(function (){
webDate(function (webTime){
get('web').innerHTML=time2String(time=webTime);
})
get('locale').innerHTML=time2String(new Date);
get('time').innerHTML=int2time(targetTime-time);
if ((targetTime-time)0) {
get('time').innerHTML = 'Game Over';
} },1000)
/script
/head
body
设定时间:2015年06月18日0时0分0秒br 服务器时间:span id='web'loading.../spanbr
本地时间:span id="locale"loading.../spanbr
倒计时时间:span id="time"loading.../span
script type="text/javascript" charset="utf-8" targetTime=new Date(2015,06,18,00,00,00); /script
/body
/html
注:原理用xmlhttp来获取服务器上的时间,后台用js做倒计时显示到页面上,由于获取服务器时间这个在本地运行不一定能成功,最好是在服务器上运行!
js如何取得服务器时间
正如楼上所说的,js只能操作客户端的东西。在客户端时刻显示并更新服务器的时间应该是不可能的,因为会由于网络延迟的因素使得时间不精确。
打个比方,可以使用ajax每秒钟获取服务器的时间,然后再显示出来,但是可能这个用户的网络延迟比较厉害,他请求的这个时间过了两三秒才返回给客户端,那这样更新时间就不精确了。
也可以这样,第一次从服务器那里获取时间,然后再客户端用js,每秒累加数,就是以获取服务器的那个时间为起掉,用客户端代码每秒累加这个时间
关于js取服务器时间显示时钟和js获取实时时间的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
本文由admin于2022-12-19发表在靑年PHP官网,如有疑问,请联系我们。
本文链接:https://www.qnphp.com/post/27709.html