1 2 Previous Next 15 Replies Latest reply on Dec 8, 2015 3:13 PM by LisaClark

    Get current client Time Zone

    CarlSchwarz

      I just thought I'd put out there this calculation to the web of how to automatically get the current time-zone difference of the client machine (relative to UTC). And it automatically adjusts for time zones etc.

       

      So for example this returns 8:00 for Perth

       

      Get(CurrentTime) -

      GetAsTime(
      GetAsTimestamp(
      Floor(Get ( CurrentTimeUTCMilliseconds ) / 1000)
      )
      )

       

      It's something that I didn't find with a search so I thought I'd add it here for reference.

        • 1. Re: Get current client Time Zone
          BowdenData

          Carl,

           

          When I do this for a machine on the east coast of the US (currently observing Daylight Savings), I get a value of "0:00 AM", which is incorrect. If I modify the calc to be:

           

          Get ( CurrentTimeStamp ) - GetAsTimestamp ( Floor ( Get ( CurrentTimeUTCMilliseconds ) / 1000 ) )

           

          it correctly returns "-4:00:00". I am 4 hours behind UTC/GMT time.

           

          Can try this on your machine (in Perth) to see that it works okay.

           

          Doug

          • 2. Re: Get current client Time Zone
            CarlSchwarz

            Yes that works well here in Perth also!  That's much neater.

            • 3. Re: Get current client Time Zone
              BowdenData

              Cool!

               

              Doug

              • 4. Re: Get current client Time Zone
                LisaClark

                Okay, so i've been scouring the internet for a way to display a real-time clock that would be able to take a time zone input (from a work location) and show the time in the place where the work was being performed.

                 

                I can calculate the time at the work site no problem  but the client also wants to see a live clock for job management and planning purposes. I'm stumped on how to have it continuously update. Been looking for web based options but have not found any yet where i can supply the time zone.

                 

                Any suggestions?

                 

                thanks

                L Clark

                • 5. Re: Get current client Time Zone
                  TimAnderson

                  This what you are looking for Lisa

                   

                   

                  https://www.briandunning.com/cf/1079

                  • 6. Re: Get current client Time Zone
                    LisaClark

                    Thanks Tim,

                    I don't see a way to supply a time zone into the custom function.

                    The issue is that the customer service rep will possibly be in a different time zone than where the work is being done so the ability to supply a time zone that is different than the users and show the current time in that other time zone is the missing piece.

                    Any thoughts about that?

                     

                    thanks

                    Lisa

                    • 7. Re: Get current client Time Zone
                      LisaClark

                      I also found this, which is pretty cool, but again, it runs off the users machine clock and no way to specify other time zone.

                      Free Clocks for Your Website

                      • 8. Re: Get current client Time Zone
                        TimAnderson

                        Hi Lisa,

                         

                        If you know the time difference, possibly using Doug's method above, you just need to apply the hours difference to the currentHours variable.

                         

                        Tim

                        • 9. Re: Get current client Time Zone
                          erolst

                          Lisa –

                           

                          assuming that you're using the CF from Dunning's, and that you've put Doug's code into a CF called TimeZoneOffset, add

                           

                          currentHours = currentHours + " & Hour ( TimeZoneOffset) & "%12;¶ (see Edit below!)

                           

                          after the line where currentHours is declared ('var CurrentHours = …)

                           

                          Oliver

                           

                          EDIT: Oops, operator precedence … must read:

                           

                          currentHours = (currentHours + " & Hour ( TimeZoneOffset) & ")%12;¶


                          PS: You could also use the getTimezoneOffset() method of the JavaScript Date object.

                          • 10. Re: Get current client Time Zone
                            LisaClark

                            thank you erolst.

                            I tried what you suggested and while i do get the clock displayed showing different times,  when i select a timeZone and a value for daylight savings or not, the times that show for the selected time zone are not correct. Since i am not a javascript programmer --strictly stick to filemaker-- i have no clue what is wrong with it. It's just like the math isn't working. I'm feeding in negative numbers for the time zone offset  so "adding" the offsets should come out the same as subtracting but that is not what is happening.

                            For 5:30 AM UTC, i get 3:30 AM for pacific time which if working correctly should be 10:30 PM.

                            The offset for pacific time (Daylight svgs) is -7 hours so i don't know how   + (-7 hrs) would get you to  3:30 am.

                            Any further suggestions?

                            Thank you in advance.

                             

                            --Lisa

                             

                             

                            Here is the revised custom function i am using:

                             

                            Name: ClockTimeZone

                            parameters:  timeZone,  daylightSvgsYN

                             

                             

                            Let ( [

                            TZ = Lower ( timeZone ) ;

                            DST = daylightSvgsYN ;

                            TimeZoneOffset =

                            Case (

                            TZ= "newfoundland" and DST = "yes" ; "-2.5" ;

                            TZ= "newfoundland" and DST = "no" ; "-3.5" ;

                            TZ= "atlantic" and DST = "yes" ; "-3" ;

                            TZ= "atlantic" and DST = "no" ; "-4" ;

                            TZ= "eastern" and DST = "yes" ; "-4" ;

                            TZ= "eastern" and DST = "no" ; "-5" ;

                            TZ= "central" and DST = "yes" ; "-5" ;

                            TZ= "central" and DST = "no" ; "-6" ;

                            TZ= "mountain" and DST = "yes" ; "-6" ;

                            TZ= "mountain" and DST = "no" ; "-7" ;

                            TZ= "pacific" and DST = "yes" ; "-7" ;

                            TZ= "pacific" and DST = "no" ; "-8" ;

                            TZ= "alaska" and DST = "yes" ; "-8" ;

                            TZ= "alaska" and DST = "no" ; "-9" ;

                            TZ= "hawaii" and DST = "yes" ; "-9" ;

                            TZ= "hawaii" and DST = "no" ; "-10" ;

                            0 )

                            ] ;

                             

                             

                            //calculation

                             

                            "data:text/html,"&

                             

                             

                            "<html>¶

                            <head>¶

                            <title>JavaScript Clock</title>¶

                            <style type=\"text/css\">¶

                            #clock { font-family: Arial, Helvetica, sans-serif; font-size: 0.8em; color: white; background-color: black; border: 2px solid purple; padding: 4px; }¶

                            </style>¶

                            <script type=\"text/javascript\">¶

                            <!--¶

                            function init ( )¶

                              timeDisplay = document.createTextNode ( \"\" );¶

                              document.getElementById(\"clock\").appendChild ( timeDisplay );¶

                            function updateClock ( )¶

                              var currentTime = new Date ( );¶

                              var currentHours = currentTime.getHours ( );¶

                             

                            currentHours = (currentHours + " & Hour ( TimeZoneOffset) & ")%12;¶

                             

                              var currentMinutes = currentTime.getMinutes ( );¶

                              var currentSeconds = currentTime.getSeconds ( );¶

                              // Pad the minutes and seconds with leading zeros, if required¶

                              currentMinutes = ( currentMinutes < 10 ? \"0\" : \"\" ) + currentMinutes;¶

                              currentSeconds = ( currentSeconds < 10 ? \"0\" : \"\" ) + currentSeconds;¶

                              // Choose either \"AM\" or \"PM\" as appropriate¶

                              var timeOfDay = ( currentHours < 12 ) ? \"AM\" : \"PM\";¶

                              // Convert the hours component to 12-hour format if needed¶

                              currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;¶

                              // Convert an hours component of \"0\" to \"12\"¶

                              currentHours = ( currentHours == 0 ) ? 12 : currentHours;¶

                              // Compose the string for display¶

                              var currentTimeString = currentHours + \":\" + currentMinutes + \":\" + currentSeconds + \" \" + timeOfDay;¶

                              // Update the time display¶

                              document.getElementById(\"clock\").firstChild.nodeValue = currentTimeString;¶

                            // -->¶

                            </script>¶

                            </head>¶

                            <body onload=\"updateClock(); setInterval('updateClock()', 1000 )\">¶

                            <div style=\"clear: both;\"> </div>¶

                             

                             

                            <div style=\"width: 10em; text-align: left; margin: 0px auto;\">¶

                              <span id=\"clock\"> </span>¶

                            </div>¶

                            </body>¶

                            </html>"

                             

                            // end calculation

                            )

                            • 11. Re: Get current client Time Zone
                              erolst

                              LisaClark wrote:

                              Since i am not a javascript programmer --strictly stick to filemaker-- i have no clue what is wrong with it.

                               

                              Here

                               

                              currentHours = (currentHours + " & Hour ( TimeZoneOffset) & ")%12;¶

                               

                              you must not use the originally suggested Hour(), since you're already calculating the result in hours (by hand), rather than as a Time from which you'd have to extract the hours; i.e.:

                               

                              currentHours = (currentHours + " & TimeZoneOffset & ")%12;¶


                              Let me also suggest a simplification for the TimeZoneOffset calculation:

                               

                               

                              Let ( [

                                TZ = Lower ( timeZone ) ;

                                DST = daylightSvgsYN ;

                                TimeZoneOffset =

                                  Case (

                                    TZ = "newfoundland" ; -3.5 ;

                                    TZ = "atlantic" ; -4 ;

                                    TZ = "eastern" ; -5 ;

                                    TZ = "central" ; -6 ;

                                    TZ = "mountain" ; -7 ;

                                    TZ = "pacific" ; -8 ;

                                    TZ = "alaska" ; -9 ;

                                    TZ = "hawaii" ; -10

                                  )

                                + ( daylightSvgsYN = "yes" )

                                ] ;

                              • 12. Re: Get current client Time Zone
                                LisaClark

                                That did not work either. Still have wacky numbers and now there's a "-" in front of the time.

                                I did not simplify generating the time zone offset -- it has to work for those oddball states like AZ and Hawaii that don't observe daylight savings time when everybody else does.

                                thanks

                                Lisa

                                • 13. Re: Get current client Time Zone
                                  erolst

                                  LisaClark wrote:

                                  I did not simplify generating the time zone offset -- it has to work for those oddball states like AZ and Hawaii that don't observe daylight savings time when everybody else does.

                                  The calc I suggested does exactly what your original one does.

                                   

                                  Try this one; note that you need to specify the time difference between the locale where the data object is created (i.e. the Web Viewer code is executed), and the other locales; e.g. if management sits in central, then that is your “time zero”, and “hawaii” would be -4, not -10; etc.

                                   

                                  Let ( [

                                    TZ = Lower ( timeZone ) ;

                                    TimeZoneOffset =

                                      Case (

                                        TZ = "newfoundland" ; -3.5 ;

                                        TZ = "atlantic" ; -4 ;

                                        TZ = "eastern" ; -5 ;

                                        TZ = "central" ; -6 ;

                                        TZ = "mountain" ; -7 ;

                                        TZ = "pacific" ; -8 ;

                                        TZ = "alaska" ; -9 ;

                                        TZ = "hawaii" ; -10

                                      )

                                    + ( daylightSvgsYN = "yes" ) // add 1 to result of case if true

                                    ] ;

                                   

                                  "data:text/html,"&

                                  "<html>¶

                                  <head>¶

                                  <title>JavaScript Clock</title>¶

                                  <style type=\"text/css\">¶

                                  #clock { font-family: Arial, Helvetica, sans-serif; font-size: 0.8em; color: white; background-color: black; border: 2px solid purple; padding: 4px; }¶

                                  </style>¶

                                  <script type=\"text/javascript\">¶

                                  <!--¶

                                  function init ( )¶

                                    timeDisplay = document.createTextNode ( \"\" );¶

                                    document.getElementById(\"clock\").appendChild ( timeDisplay );¶

                                  function updateClock ( )¶

                                    var currentTime = new Date ( );¶

                                    var currentHours = currentTime.getHours ( );¶

                                    currentHours = Math.abs(currentHours + " & TimeZoneOffset & ")%24;¶

                                    var currentMinutes = currentTime.getMinutes ( );¶

                                    var currentSeconds = currentTime.getSeconds ( );¶

                                    // Pad the minutes and seconds with leading zeros, if required¶

                                    currentMinutes = ( currentMinutes < 10 ? \"0\" : \"\" ) + currentMinutes;¶

                                    currentSeconds = ( currentSeconds < 10 ? \"0\" : \"\" ) + currentSeconds;¶

                                    // Choose either \"AM\" or \"PM\" as appropriate¶

                                    var timeOfDay = ( currentHours < 12 ) ? \"AM\" : \"PM\";¶

                                    // Convert the hours component to 12-hour format if needed¶

                                    currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;¶

                                    // Convert an hours component of \"0\" to \"12\"¶

                                    currentHours = ( currentHours == 0 ) ? 12 : currentHours;¶

                                    // Compose the string for display¶

                                    var currentTimeString = currentHours + \":\" + currentMinutes + \":\" + currentSeconds + \" \" + timeOfDay;¶

                                    // Update the time display¶

                                    document.getElementById(\"clock\").firstChild.nodeValue = currentTimeString;¶

                                  // -->¶

                                  </script>¶

                                  </head>¶

                                  <body onload=\"updateClock(); setInterval('updateClock()', 1000 )\">¶

                                  <div style=\"clear: both;\"> </div>¶

                                  <div style=\"width: 10em; text-align: left; margin: 0px auto;\">¶

                                    <span id=\"clock\"> </span>¶

                                  </div>¶

                                  </body>¶

                                  </html>"

                                  )

                                  • 14. Re: Get current client Time Zone
                                    LisaClark

                                    Thank you! I will try your suggestion. I did not see this 6 months ago--other projects came to the fore and the time question got set aside for a while.

                                    --Lisa

                                    1 2 Previous Next