Javascript 将时间从服务器传递到客户端

Javascript 将时间从服务器传递到客户端,javascript,Javascript,我正在使用图像和javascript创建一个数字时钟,但我想在这个时间内传递一个服务器时间。。。如何做到这一点。。。我从服务器获取时间,并将其传递到当前日期 下面我给出了一个片段 var time_str = document.clock_form.time_str.value ; //alert (time_str); function dotime(){ theTime=setTimeout('dotime();',1000); //d = new Date(Dat

我正在使用图像和javascript创建一个数字时钟,但我想在这个时间内传递一个服务器时间。。。如何做到这一点。。。我从服务器获取时间,并将其传递到当前日期

下面我给出了一个片段

    var time_str = document.clock_form.time_str.value ; //alert (time_str);
function dotime(){ 
    theTime=setTimeout('dotime();',1000);
    //d = new Date(Date.parse(time_str));
    d= new Date(time_str);
    hr= d.getHours()+100;
    mn= d.getMinutes()+100;
    se= d.getSeconds()+100; var time_str = document.clock_form.time_str.value ; //alert (time_str);
    alert(' TIME --->   '+hr+' :: '+mn+' :: '+ se);

    if(hr==100){
        hr=112;am_pm='am';
    }
    else if(hr<112){
        am_pm='am';
    }
    else if(hr==112){
        am_pm='pm';
    }
    else if(hr>112){
        am_pm='pm';hr=(hr-12);
    }
    tot=''+hr+mn+se;
    document.hr1.src = '/flash_files/digits/dg'+tot.substring(1,2)+'.gif';
    document.hr2.src = '/flash_files/digits/dg'+tot.substring(2,3)+'.gif';
    document.mn1.src = '/flash_files/digits/dg'+tot.substring(4,5)+'.gif';
    document.mn2.src = '/flash_files/digits/dg'+tot.substring(5,6)+'.gif';
    document.se1.src = '/flash_files/digits/dg'+tot.substring(7,8)+'.gif';
    document.se2.src = '/flash_files/digits/dg'+tot.substring(8,9)+'.gif';
    document.ampm.src= '/flash_files/digits/dg'+am_pm+'.gif';
}
dotime();
var time\u str=document.clock\u form.time\u str.value//警报(时间);
函数dotime(){
时间=设置超时('dotime();',1000);
//d=新日期(Date.parse(time_str));
d=新日期(时间);
hr=d.getHours()+100;
mn=d.getMinutes()+100;
se=d.getSeconds()+100;var time\u str=document.clock\u form.time\u str.value;//警报(time\u str);
警报('TIME-->'+hr+':'+mn+':'+se);
如果(hr==100){
hr=112;am_pm='am';
}
否则,如果(hr112){
am_pm='pm';hr=(hr-12);
}
tot=''+hr+mn+se;
document.hr1.src='/flash_files/digits/dg'+tot.substring(1,2)+'.gif';
document.hr2.src='/flash_files/digits/dg'+tot.substring(2,3)+'.gif';
document.mn1.src='/flash_files/digits/dg'+tot.substring(4,5)+'.gif';
document.mn2.src='/flash_files/digits/dg'+tot.substring(5,6)+'.gif';
document.se1.src='/flash_files/digits/dg'+tot.substring(7,8)+'.gif';
document.se2.src='/flash_files/digits/dg'+tot.substring(8,9)+'.gif';
document.ampm.src='/flash_files/digits/dg'+am_pm+'.gif';
}
dotime();
但它不起作用

帮帮我


提前感谢。

您从服务器传递的是什么?它是Unix时间戳还是格式化日期

如果要传递Unix时间戳,那么在JavaScript中,您将执行以下操作:

var date = new Date(timestamp * 1000);
您必须将其乘以1000,因为Unix时间戳表示自1970年1月1日00:00:00以来经过的秒数,而JavaScript
Date
构造函数预期自1970年1月1日00:00:00以来经过的毫秒数

如果要传递格式化的日期字符串,可以用JavaScript对其进行如下解析:

var date = Date.parse(formated_date);
<?php
$js_date = date(DATE_RFC1123);
日期应采用RFC 1123规定的格式。在PHP中,可能是这样的:

var date = Date.parse(formated_date);
<?php
$js_date = date(DATE_RFC1123);

“但它不起作用”:你能解释一下什么不起作用吗?你有错误吗?错误是什么?调试警报是否显示?它们显示什么?document.clock\u form.time\u str.value
中的值是什么样的?定义为“不工作”。有错误吗?
time\u str
是JavaScript可解析格式吗?你真的需要用
var
+1定义所有这些局部变量,我本打算提出这个建议,但正在等待OP的更多信息。我是perl用户,我正以这种格式获取时间(RFC1123),例如“Fri,04 Jun 2010 17:46:14+0530”所以“time_str”值将如示例所示。。。当您提醒它发出错误的时间i am perl user,&我正在以这种格式获取时间(RFC1123),例如“Fri,04 Jun 2010 17:46:14+0530”,因此“time_str”值将如示例所示。。。当你提醒时间出错时,我不知道为什么,但我认为你会出错,因为你在函数中重新声明了时间。删除它前面的var。