views:

46

answers:

1

Dear all, i am creating a digital clock using images & javascript but i want to pass a server time that time ... How to do that ... I am getting time from server and passing it to Date

Following i have given snippet .

    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();

But it is not working

Help me out

Thanks in advance.

+1  A: 

What is it that you're passing from the server? Is it a Unix timestamp, or a formated date?

If you're passing an Unix timestamp, then in JavaScript you'll do something like:

var date = new Date(timestamp * 1000);

You have to multiply it by 1000, because the Unix timestamp means the number of seconds elapsed since 1 January 1970 00:00:00 while the JavaScript Date constructor expects the number of milliseconds since 1 January 1970 00:00:00.

If you're passing a formated date string, you can parse it in JavaScript like this:

var date = Date.parse(formated_date);

The date should be in the format described by the RFC 1123. In PHP that could be something like this:

<?php
$js_date = date(DATE_RFC1123);
Oblio
+1, I was going to suggest this, but was waiting for more information from the OP.
Andy E
I don't know why, but I think you're getting an error because you're redeclaring time_str in your function. Remove the var in front of it.
Oblio