12 Replies Latest reply on Jan 22, 2012 12:53 AM by Gingernut

    Javascript dates

    Gingernut

      Alarm Clock in webviewer using Fields for alert times

       

      I have been trying to use Inspired Business Suite Javascript in a Filemaker file that basically sets a timer and or alarm It works perfectly in the file I was given to dowload USA based but when I import it into my local UK file it does not work The Timer is prob easiest example to show

       

      When I add 1 minute in UK file it shows 13145 hours plus 1 minute

       

      How do I convert the date formats please

       

      I am not at all familiar with Java (but can work out what is being done) my timestamps etc all look ok in the local file IE shows full date and time as 21st January 2012 15:23

      Thanks in anticipation

       

      Cathy

      "data:text/html,<html>

      <head>

      <script type='text/javascript'>

       

      /*Setting JavaScript Variables to value of FileMaker fields*/

      var targetTimeTxt = '" & Timer::Timestamp_End & "';

      var status = '" & Timer::Status & "';

      var labelTxt = '" & Timer::Label & "';

      /*End FileMaker Fields*/

       

      var currentTime = new Date();

      var targetTime = new Date(targetTimeTxt);

      var remaining = Math.floor((targetTime - currentTime)/1000);

       

      function setClock()

      {

      var currentTime = new Date();

      var clock = document.getElementById('clock');

      var labelobj = document.getElementById('label1');

      var secondsRemaining=0;

       

      if(labelTxt=='')

      {

      labelobj.innerHTML='Timer';

      }

      else

      {

      labelobj.innerHTML=labelTxt;

      }

       

      if (targetTime>currentTime)

      {

      secondsRemaining=Math.floor((targetTime - currentTime)/1000);

      }

       

      var hours = Math.floor( secondsRemaining / 3600 );

      var minutes = Math.floor((secondsRemaining%3600) / 60 );

      if(minutes<10)minutes='0' + minutes;

      var seconds = secondsRemaining%60;

      if(seconds<10)seconds='0'+seconds;

       

      clock.innerHTML=hours + ':' + minutes + ':' + seconds;

       

      if(targetTimeTxt=='' || status=='Acknowledged' || ( secondsRemaining==0 && Math.floor(currentTime/1000)%2==0 ) )

      {

      document.body.style.backgroundColor='#FFFFFF';

      if ( targetTimeTxt=='' || status=='Acknowledged' )

      {

      clock.innerHTML='--:--:--';

      }

      }

      else if(secondsRemaining==0)

      {

      document.body.style.backgroundColor='#FFFF00';

      document.getElementById('sound1').Play();

      }

       

      setTimeout('setClock();',1000);

      }

       

      </script>

      </head>

      <body style='margin:4px;padding:0;font-size:14px;font-weight:bold;font-family: Arial, Helvetica, sans-serif;text-align:center;background-color:#FFFFFF;' onload='setClock();'>

      <div id='label1' style='font-size:10px;font-weight:bold;'>

      </div>

      <div id='clock'>

      </div>

      <embed src='file:///System/Library/Sounds/Glass.aiff' autostart='false' id='sound1'

      enablejavascript='true' width='0' height='0'>

      </body></html>"

        • 1. Re: Javascript dates
          LyndsayHowarth

          Hi Cathy,

           

          I am going to make 2 suggestions... no guarantees...

           

          1. Under this line : var currentTime = new Date(); try pasting this line

          var currentTime = currentTime.toLocaleDateString();

          and under this line : var targetTime = new Date(targetTimeTxt); try pasting

          var targetTime = targetTime.toLocaleDateString();

           

          2. What's in the field Timer::Label ? If that is the date... you can try format it in FIleMaker...

           

          HTH

          - Lyndsay

          • 2. Re: Javascript dates
            comment

            Try changing this line:

            var targetTimeTxt = '" & Timer::Timestamp_End & "';

            into:

             

            var targetTimeTxt = " & 1000 * ( Timer::Timestamp_End - 62135596800 ) & ";
            
            • 3. Re: Javascript dates
              Gingernut

              Thank you will try that the label is just the contents of a field that get written into the clock field when the alarm goes off to say the reason for the alert

              • 4. Re: Javascript dates
                Gingernut

                this just made the timer go off instantly with no value - what was the value deducting in this case please

                 

                Thank for your time

                Cathy

                • 5. Re: Javascript dates
                  Gingernut

                  unfortunately this did not work either as it then did not count down but went straight to alarm mode

                   

                  I am going to have to do some serious learning as I am not fully understanding what all the functions do = will try changing in th clock part and see if that helps

                   

                  Here is the code for the clock which is more what I am interested in but I thought if I could see why Timer failed might understand the clock better

                  result in data viewer shows date in UK format not US

                   

                  "data:text/html,<html>

                  <head>

                  <script type='text/javascript'>

                   

                  var alarmTimeTxt = '" & Alarm::Timestamp & "';

                  var acknowledgeTimeTxt = '" & Alarm::AcknowledgeTimeStamp & "';

                  var alarmLabel = '" & Alarm::Label & "';

                   

                   

                  if (alarmLabel=='') alarmLabel='Alarm';

                  var alarmTime = new Date(alarmTimeTxt);

                  var acknowledgeTime = new Date(acknowledgeTimeTxt);

                   

                  function setClock()

                  {

                      var currentTime = new Date();

                      var clock = document.getElementById('clock');

                   

                   

                      if (alarmTimeTxt=='' || alarmTime >= currentTime || (acknowledgeTimeTxt !='' && acknowledgeTime >= alarmTime) || Math.floor(currentTime/1000)%2==0 )

                      {

                          document.body.style.backgroundColor='#FFFFFF';

                          clock.innerHTML=currentTime.toLocaleDateString() + ' ' + currentTime.toLocaleTimeString();

                      }

                      else

                      {

                          document.body.style.backgroundColor='#FFFF00';

                          clock.innerHTML=alarmLabel + \"<embed src='file:c:/windows/media/chimes.wav' autostart='true'  id='sound1'

                  enablejavascript='true' width='0' height='0'>\";

                   

                          /*document.getElementById('sound1').Play();*/

                   

                      }

                     

                      setTimeout('setClock();',1000);

                  }

                   

                  </script>

                  </head>

                  <body style='margin:4px;padding:0;font-size:12px;font-weight:bold;font-family: Arial, Helvetica, sans-serif;text-align:center;background-color:#FFFFFF;' onload='setClock();'>

                  <p id='clock'>

                  </p>

                  </body></html>"

                  • 6. Re: Javascript dates
                    comment

                    Gingernut wrote:

                     

                    this just made the timer go off instantly with no value

                    I am not sure what you mean by that - does this work for you?

                    • 7. Re: Javascript dates
                      Gingernut

                      No I am afraid not it did not set any value into the timer,

                      I have been struggling with this all day and eventually resorted to script master Call script at scheduled time but even this is giving me problems running anything between 3 and 5 minutes later than expected. My last resort will be install on timer but don't want to see screen refresh that seems to occur with that

                      Thank you for trying to help

                      Cathy

                      • 8. Re: Javascript dates
                        comment

                        Gingernut wrote:

                         

                        No I am afraid not it did not set any value into the timer,

                         

                        That's really weird. How about the file posted here:

                        http://fmforums.com/forum/topic/71934-calculating-elapsed-time-realtime/page__p__340205#entry340205

                        • 9. Re: Javascript dates
                          Gingernut

                          Will check that out in the morning thank you. Brain frazzled right now but that looks promising

                          1 of 1 people found this helpful
                          • 10. Re: Javascript dates
                            jrenfrew

                            Cathy

                             

                            I think it is not converting the FM timestamp to a Javascript time properly

                             

                            try

                            var alarmTimeTxt = '" & MonthName(Alarm::Timestamp) & " " & Day(Alarm::Timestamp) & ", "  & Year(Alarm::Timestamp) & " " & GetAsTime(Alarm::Timestamp) & "';

                             

                            This is because the original version gave an Invalid Date so alarmTime >= currentTime will always be false.

                            Seems to then work in the quick demo I knocked up.

                             

                            Or here is a custom function to convert FM timestamp to UTC javascript time milliseconds

                             

                            /*

                            * javascriptTime( thisTimestamp )

                            * 12_01_21 JR

                            * v1.0

                            *

                            * input: (time)

                            * return: local time as javascript milliseconds timestamp

                            *

                            */

                             

                            Let ( [

                                      _time = If ( thisTimestamp = "" or thisTimestamp = "now" ; Get ( CurrentTimeStamp ) ;  GetAsTimestamp ( thisTimestamp ) ) ;

                                      _unix = GetAsNumber ( _time ) - GetAsNumber ( GetAsTimestamp ( Date ( 1 ; 1 ; 1970 ) ) )

                            ] ;

                                      _unix * 1000

                            )

                             

                             

                             

                            Then try var alarmTimeTxt = " & javascriptTime(Alarm::Timestamp) & " ; - WITHOUT the single quotes

                             

                             

                            John

                            • 11. Re: Javascript dates
                              LyndsayHowarth

                              I believe the problem is in the formatting of the timestamp... it is expressed in US format... which is why it worked originally with the wrong locale for you.

                               

                              Maybe I applied the "toLocaleDateString()" to the wrong thing... perhaps it should have been applied to the timestamp.

                               

                              - Lyndsay

                              1 of 1 people found this helpful
                              • 12. Re: Javascript dates
                                Gingernut

                                John

                                 

                                 

                                 

                                You are such a star it works

                                 

                                 

                                 

                                I had spent almost 24 hours on it cos I really wanted to work it out myself but could not quite get there (as always I did learn a load of other stuff enroute)

                                 

                                 

                                 

                                I cannot tell you how much I appreciate you taking the time to do this in the middle of the night (almost).

                                 

                                 

                                 

                                Good that you are keeping busy I guess but there is more to life than work so give yourself some sanity time.

                                 

                                I used the first option BTW

                                 

                                 

                                 

                                Have a good weekend I am going to be working too to fit this into my file properly

                                 

                                Thanks again

                                 

                                 

                                 

                                Cathy