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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gskoli
    New Member
    • May 2010
    • 23

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

    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 :
    Code:
    function dotime(){ 
            theTime=setTimeout('dotime();',10000);
            d = new Date();
            hr= d.getHours()+100;
            mn= d.getMinutes()+100;
            se= d.getSeconds()+100; var time_str = document.clock_form.time_str.value ; //alert (time_str);
            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();
    Last edited by Niheel; Jun 1 '10, 11:05 AM. Reason: Don't use bold unnecessarily
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    just print the timestamp (RFC1123/RFC822 or Milliseconds) into the Date() constructor.

    Comment

    • gskoli
      New Member
      • May 2010
      • 23

      #3
      Thanks for reply, But not getting you. Can u give me some example or snippet?
      Last edited by Niheel; Jun 1 '10, 11:05 AM. Reason: spelling, grammar, punctuation

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        I don’t know how to do it in Perl, but I could give you an example in PHP.
        Code:
        function dotime(){ 
                theTime=setTimeout(dotime,10000);
                d = new Date([B]"<?php echo date("r"); ?>"[/B]);
                hr= d.getHours()+100;
                mn= d.getMinutes()+100;
                // etc.

        Comment

        • gskoli
          New Member
          • May 2010
          • 23

          #5
          Originally posted by Dormilich
          just print the timestamp (RFC1123/RFC822 or Milliseconds) into the Date() constructor.
          Can you elaborate it please?
          Last edited by Niheel; Jun 1 '10, 11:04 AM. Reason: please use full sentences, punctuation and proper spelling

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            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).

            Comment

            • gskoli
              New Member
              • May 2010
              • 23

              #7
              Thanks for reply ...

              Comment

              • gskoli
                New Member
                • May 2010
                • 23

                #8
                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..

                Comment

                Working...