469,289 Members | 2,137 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,289 developers. It's quick & easy.

How to get Server time & pass to javascript to display clock

gskoli
23
I have created a gif based digital clock but problem is that it will show me the client side time . I am a perl developer so i am getting server time, i mean , i can fetch that time , to pass the javascript .
Can we do that i will fetch the server time only once and from that will create time .

snippet of code given below :
Expand|Select|Wrap|Line Numbers
  1. function dotime(){ 
  2.         theTime=setTimeout('dotime();',10000);
  3.         d = new Date();
  4.         hr= d.getHours()+100;
  5.         mn= d.getMinutes()+100;
  6.         se= d.getSeconds()+100; var time_str = document.clock_form.time_str.value ; //alert (time_str);
  7.         if(hr==100){
  8.                 hr=112;am_pm='am';
  9.         }
  10.         else if(hr<112){
  11.                 am_pm='am';
  12.         }
  13.         else if(hr==112){
  14.                 am_pm='pm';
  15.         }
  16.         else if(hr>112){
  17.                 am_pm='pm';hr=(hr-12);
  18.         }
  19.         tot=''+hr+mn+se;
  20.         document.hr1.src = '/flash_files/digits/dg'+tot.substring(1,2)+'.gif';
  21.         document.hr2.src = '/flash_files/digits/dg'+tot.substring(2,3)+'.gif';
  22.         document.mn1.src = '/flash_files/digits/dg'+tot.substring(4,5)+'.gif';
  23.         document.mn2.src = '/flash_files/digits/dg'+tot.substring(5,6)+'.gif';
  24.         document.se1.src = '/flash_files/digits/dg'+tot.substring(7,8)+'.gif';
  25.         document.se2.src = '/flash_files/digits/dg'+tot.substring(8,9)+'.gif';
  26.         document.ampm.src= '/flash_files/digits/dg'+am_pm+'.gif';
  27. }
  28. dotime();
  29.  
  30.  
Jun 1 '10 #1
7 3718
Dormilich
8,651 Expert Mod 8TB
just print the timestamp (RFC1123/RFC822 or Milliseconds) into the Date() constructor.
Jun 1 '10 #2
gskoli
23
Thanks for reply, But not getting you. Can u give me some example or snippet?
Jun 1 '10 #3
Dormilich
8,651 Expert Mod 8TB
I don’t know how to do it in Perl, but I could give you an example in PHP.
Expand|Select|Wrap|Line Numbers
  1. function dotime(){ 
  2.         theTime=setTimeout(dotime,10000);
  3.         d = new Date("<?php echo date("r"); ?>");
  4.         hr= d.getHours()+100;
  5.         mn= d.getMinutes()+100;
  6.         // etc.
Jun 1 '10 #4
gskoli
23
@Dormilich
Can you elaborate it please?
Jun 1 '10 #5
Dormilich
8,651 Expert Mod 8TB
description of the Date() construtor.

if you want to feed JavaScript with a time from the server, you have to pass the Date() constructor a date/time (see above). however, JS by default uses local timezone settings for its output, unless you use Date’s getUTC* methods. (also listed in the link above).
Jun 1 '10 #6
gskoli
23
Thanks for reply ...
Jun 4 '10 #7
gskoli
23
Thanks BUddy ..
But i have tried it with that i was getting time in 822 format with passing it in hidden variable ...
But giving error ...
So can you try it out with the code something ,

Thanks In advance..
Jun 4 '10 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by dan glenn | last post: by
10 posts views Thread by RadioFreq | last post: by
2 posts views Thread by john | last post: by
5 posts views Thread by sandman | last post: by
2 posts views Thread by Daniel | last post: by
14 posts views Thread by rabbitrun | last post: by
2 posts views Thread by pradeeplnarayan | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.