6 Replies Latest reply on Apr 13, 2010 7:26 AM by Kevin Cavallo

    Countdown date to deadline

    Lucrecious

      How do I implement a countdown date to a specified dealine which will show on the form and auto adjust to NY timezone?

       

      The field should show days left to submission deadline.

        • 1. Re: Countdown date to deadline
          Kevin Cavallo Level 4

          I don't believe that there is timezone info available in FormCalc, so I worked this out in javascript.

           

          Javascript is able to get the local system time and timezone of the machine that the script is running on.  There is no way to obtain the timezone for NY from a random system, so it needs to be set as a constant in the script.  This will change from GMT-4 to GMT-5 depending on whether daylight savings time is in force, but you might want to ignore the hour difference.

           

          Here's the script to do the calculation.  You will just need to change the NY_TARGET_DATE constant to whatever date you want.

           

           

          // The following two constants must be set for your current situation

          // NY_TIME_ZONE_DATE_OFFSET = timezone difference between GMT and NY

          // NY_TARGET_DATE = the deadline date

           

          var NY_TIME_ZONE_OFFSET = -4;

          var NY_TARGET_DATE = "4/8/2010";

           

          var ONE_HOUR_IN_MS = 1000*60*60;

          var ONE_DAY_IN_MS = ONE_HOUR_IN_MS * 24;

          var nyTimeZoneOffsetMs = NY_TIME_ZONE_OFFSET * ONE_HOUR_IN_MS;

           

           

          // Obtain target NY date/time as milliseconds

           

          var targetNYDate = new Date(NY_TARGET_DATE);

          var targetNYTimeMs = targetNYDate.getTime();

           

           

          // Get current local date/time from the system as milliseconds

          var currentLocalDate = new Date();

          var currentLocalTimeMs = currentLocalDate.getTime();

           

           

          // Get offset of local system timezone from UTC. This

          // will be in minutes; convert to milliseconds by multiplying

          // by 60000 (60 sec/minute, 1000ms/second)

          var localTzOffset = currentLocalDate.getTimezoneOffset() * 60000;

           

          // Add local timezone offset to current local time to convert our local time to UTC

          var utcDate = new Date(currentLocalTimeMs += localTzOffset);

           

           

          // Calculate current NY time by adding NY offset to UTC

          var currentNYTimeMs = utcDate.getTime()+ nyTimeZoneOffsetMs;

           

          // Get difference between the target NY date/time

          // and current NY date/time in milliseconds

          var timeDifferenceMs = targetNYTimeMs - currentNYTimeMs

           

           

          // Divide by number of milliseconds in a day to get number of days.

          // Math.ceil will round up. Will return a positive number of days until

          // target date; zero if same day as target date, and

          // negative number if past the target date.

          var numDays = Math.ceil(timeDifferenceMs / ONE_DAY_IN_MS);

           

          this.rawValue = numDays;

          • 2. Re: Countdown date to deadline
            Lucrecious Level 1

            Kevin,

             

            Is there a method for setting the deadline date by the user for your script?

             

            This is due to the changing deadlines based on events.

             

            I am interested in a drop-down date field which will update the deadline date in the script provided.

             

            This script works perfectly by the way, thank you very much.

            • 3. Re: Countdown date to deadline
              Kevin Cavallo Level 4

              Yes, not a problem to implement using a date from a dropdown.  I'll post an updated script shortly; a little busy now...

              • 4. Re: Countdown date to deadline
                Lucrecious Level 1

                Thank you Kevin,

                 

                I look forward to the script.

                • 5. Re: Countdown date to deadline
                  Lucrecious Level 1

                  Kevin,

                   

                  Will you have time to provide the code to modify the deadline date within the script in the next day or two?

                   

                  Thank you for your time,

                  • 6. Re: Countdown date to deadline
                    Kevin Cavallo Level 4

                    I have an updated example but it looks like file attachments have been disabled again.  Send me a private message with your email address and I'll forward the example.