4 Replies Latest reply on Nov 1, 2013 8:57 AM by gkaiseril

    How do I add 3 hours to a timesheet

    KrisSyvertsen

      Someone once upon a time developed a great script for our timesheets. It fills in the dates in the appropriate blocks. The problem is this, if you input a general date (i.e. November 2, 2013) when it comes time for daylight savings time (like this weekend), it adds November 3rd twice. Now there is a fix by putting the date and a time past 2:00am, (i.e. November 2, 2013 03:00). So my question is this: Is there a way to add 3 hours, so it automatically thinks the time is 3am instead of midnight which is the default? Thanks in advance!

      Below is the script we have:

       


      // Get Date and Time strings
      var cStartDate = this.getField("BegDate").value;

      // Only process if field contains a value
      if(cStartDate != "")
      {
        // Convert to Date Object
        var oStartDay = new Date(cStartDate);

        // Add 1 Day to each of the date fields for the next 5 days
        var oneDay = 24 * 60 * 60 * 1000;
       
        var nNewTime = oStartDay.getTime();

      //  for(var i=0;i<5;i++)
      //  {
           var oNewTime = new Date(nNewTime);
           var cFldName = "Sat1_Date";
           this.getField(cFldName).value = util.printd("dd",oNewTime);

           nNewTime += oneDay;
           var oNewTime = new Date(nNewTime);
           var cFldName = "Sun1_Date";
           this.getField(cFldName).value = util.printd("dd",oNewTime);

           nNewTime += oneDay;
           var oNewTime = new Date(nNewTime);
           var cFldName = "Mon1_Date";
           this.getField(cFldName).value = util.printd("dd",oNewTime);

           nNewTime += oneDay;
           var oNewTime = new Date(nNewTime);
           var cFldName = "Tue1_Date";
           this.getField(cFldName).value = util.printd("dd",oNewTime);

           nNewTime += oneDay;
           var oNewTime = new Date(nNewTime);
           var cFldName = "Wed1_Date";
           this.getField(cFldName).value = util.printd("dd",oNewTime);

           nNewTime += oneDay;
           var oNewTime = new Date(nNewTime);
           var cFldName = "Thu1_Date";
           this.getField(cFldName).value = util.printd("dd",oNewTime);

           nNewTime += oneDay;
           var oNewTime = new Date(nNewTime);
           var cFldName = "Fri1_Date";
           this.getField(cFldName).value = util.printd("dd",oNewTime);

           nNewTime += oneDay;
           var oNewTime = new Date(nNewTime);
           var cFldName = "Sat2_Date";
           this.getField(cFldName).value = util.printd("dd",oNewTime);

           nNewTime += oneDay;
           var oNewTime = new Date(nNewTime);
           var cFldName = "Sun2_Date";
           this.getField(cFldName).value = util.printd("dd",oNewTime);

           nNewTime += oneDay;
           var oNewTime = new Date(nNewTime);
           var cFldName = "Mon2_Date";
           this.getField(cFldName).value = util.printd("dd",oNewTime);

           nNewTime += oneDay;
           var oNewTime = new Date(nNewTime);
           var cFldName = "Tue2_Date";
           this.getField(cFldName).value = util.printd("dd",oNewTime);

           nNewTime += oneDay;
           var oNewTime = new Date(nNewTime);
           var cFldName = "Wed2_Date";
           this.getField(cFldName).value = util.printd("dd",oNewTime);

           nNewTime += oneDay;
           var oNewTime = new Date(nNewTime);
           var cFldName = "Thu2_Date";
           this.getField(cFldName).value = util.printd("dd",oNewTime);

           nNewTime += oneDay;
           var oNewTime = new Date(nNewTime);
           var cFldName = "Fri2_Date";
           this.getField(cFldName).value = util.printd("dd",oNewTime);
           var cFldName = "EndDate";
           this.getField(cFldName).value = util.printd("mmmm dd, yyyy",oNewTime);
      //  }
      }
      else
      {
        // Reset all the form fields

      //  for(var i=0;i<5;i++)
      //  {
           this.getField(Sat1_Date).value = "";
           this.getField(Sun1_Date).value = "";
           this.getField(Mon1_Date).value = "";
           this.getField(Tue1_Date).value = "";
           this.getField(Wed1_Date).value = "";
           this.getField(Thu1_Date).value = "";
           this.getField(Fri1_Date).value = "";
           this.getField(Sat2_Date).value = "";
           this.getField(Sun2_Date).value = "";
           this.getField(Mon2_Date).value = "";
           this.getField(Tue2_Date).value = "";
           this.getField(Wed2_Date).value = "";
           this.getField(Thu2_Date).value = "";
           this.getField(Fri2_Date).value = "";
      //  }
      }

        • 1. Re: How do I add 3 hours to a timesheet
          gkaiseril MVP & Adobe Community Professional

          Have you considered making sure the starting date object is at midnight and not some other time on that date?

           

          You could also set the time for noon on the starting date.

          • 2. Re: How do I add 3 hours to a timesheet
            KrisSyvertsen Level 1

            What I am trying to avoid is putting the time on the form because it gets bulky and ugly. I was hoping to just add the hours into the script somehow so it will automatically start anytime after 2:00am so it doesn't get confused by daylight savings time.

            • 3. Re: How do I add 3 hours to a timesheet
              gkaiseril MVP & Adobe Community Professional

              Some time when one use the util.scand method or the new Date()  a time value is added even when using the date string only.  You do not need to add the time to the form, you need to adjust the date object's time values.

               

              The code "console.println(new Date());" Creates a response like:

               

              Fri Nov 01 2013 09:46:04 GMT-0500 (Central Daylight Time)

               

              true

               

              You are aware that the date object also includes time information and that information also includes the time zone offset.

               

              You are using the getTime() method and then adjusting by milliseconds. This is the approach provided by Adobe's documentation. You could also use the setDate and getDate methods to adjust the date.

              • 4. Re: How do I add 3 hours to a timesheet
                gkaiseril MVP & Adobe Community Professional

                You might find the following post in a forum helpful:

                 

                Javascript dates: what is the best way to deal with Daylight Savings Time?

                 

                Daylight Saving Time

                 

                Instead of creating a new property for the date object you can create a funciton:

                 

                function stdTimezoneOffset () {

                var jan = new Date(new Date().getFullYear(), 0, 1);

                var jul = new Date(new Date().getFullYear(), 6, 1);

                return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());

                }

                 

                That returns the timezone offset when standard time is in effect in minutes,

                 

                I would consider adjusting the time for the date object to be the same or later than the timezone offset when standard time is in effect.