You are viewing limited content. For full access, please sign in.

Question

Question

Trying to add a week to a date

asked on March 24, 2017 Show version history

I have an LF Form that allows a date in a box. Then I have a date that is supposed to add a week from whatever the date happens to be in the first box. For testing purposes I always had the first box to the current date. I then followed a tutorial and just added 7 days to the current date. 

 

Today I found out that with the script that I have, if today was '2017-06-24' then the next week box is set to '2017-06-31', but there are only 30 days in June. This isn't just incrementing because if I add 8 days instead of 7, it goes to '2017-07-01'. Any ideas on how to adjust the code below?

 

    // Raul - Add a week // This is the "VERIFY" process
    var datePlusOneWeek = new Date();
    datePlusOneWeek.setDate(datePlusOneWeek.getDate() + 7); //+ 7 makes june and september 31 days
 	var dd_datePlusOneWeek = datePlusOneWeek.getDate();
    //var dd_datePlusOneWeek = ('0' + (datePlusOneWeek.getDate()+parseInt(tSpan)+1)).slice(-2); //Working on this
    var mm_datePlusOneWeek = ('0' + (datePlusOneWeek.getMonth()+parseInt(tSpan)+1)).slice(-2); //January is 0! -----// month with leading zeros ('0' + (MyDate.getMonth()+1)).slice(-2)
	var yyyy_datePlusOneWeek = datePlusOneWeek.getFullYear();
    var Rerformatted_datePlusOneWeek = yyyy_datePlusOneWeek+'-'+mm_datePlusOneWeek+'-'+dd_datePlusOneWeek;
    
     $(".DateToVerify input").val(Rerformatted_datePlusOneWeek);
    // Raul - Add a week // This is the "VERIFY" process

 

0 0

Answer

SELECTED ANSWER
replied on March 24, 2017

Can you use the built-in calculations in Forms instead? You can just set the second date field to be the first date field plus seven.

1 0
replied on March 24, 2017

10.1?

0 0
replied on March 24, 2017

Yes, the above will work with Forms 10.1

1 0
replied on March 27, 2017 Show version history

This works much better. I actually have a date box and then I had 2 date boxes after and 3 date boxes before the "Main" date. The 2 after had to be set to 7, and 14 days after the Main date box and the previous 3 boxes had to be set to 7,14 and 28 days before the main date. This really made it much easier. 

 

On thing that I'd like to point out is that this did not work for me using a regular field. I had to update the boxes to be a Date (calendar) field. Also the boxes, other then the Main date field do not populate automatically, unless you change the main date field. For that I just added a piece of code on a radio button (which we already had anyways) that had to be clicked before all the dates appear. 

 

$(".DateCalculated2 input").change();  // this is the main date box.

 

Here is the second question...those radio buttons are set 3 and 6. So the user pulls the main date from the database and then if the user chose to change it it can updated it to Main Date field + 3 or + 6 months. Any build in function for that? I currently have:

 

        // Raul - date function should be in the format of 2017-02-23
        var today = new Date();
        var dd = today.getDate();
        //var mm = today.getMonth()+1; //January is 0! ---- // month witout leading zeros //---- use parseInt(tSpan) to convert tSpan to integer
        var mm = ('0' + (today.getMonth()+parseInt(tSpan)+1)).slice(-2); //January is 0! -----// month with leading zeros ('0' + (MyDate.getMonth()+1)).slice(-2)

        var yyyy = today.getFullYear();

        //var present = mm+'/'+dd+'/'+yyyy;
        //var present = '2017-09-20';
        var present = yyyy+'-'+mm+'-'+dd;

where parseInt(tSpan) is the number chosen (3 or 6) months.

0 0
replied on March 27, 2017

This works fine for now for adding months. The only thing I changed was to use this format instead.

 

var present = mm+'/'+dd+'/'+yyyy;

0 0
replied on March 31, 2017

Ok, I'm back on this project and today I found an issue with the Main Date as well.

 

For example, if I add 3 months to today's date '3/31/2017', my script converts it into '6/31/2017' which breaks the other 5 dates that depended on it because there is no 31 days in June.

For the rest of the boxes it worked using the sample from Alexander. This date can't be hard-coded because the user has to choose to increase the Main date by 3 or 6 months.

This is the basic code.

      $(document).on('click', '.prefSched input', recalSchedDate); //when the radio button for 3 or 6 moths is clicked 
      
function recalSchedDate() { 

         var tSpan = $(this).val();   // the falue of the radio button
         $('.prefSched_box input').val(tSpan); // this will add then number of months (3 or 6) to today
         var prefSched_box = $('.prefSched_box input').val();


        var today = new Date();
        var dd = today.getDate();
        var mm = today.getMonth()+1+parseInt(tSpan); //January is 0! ---- // month witout leading zeros //---- use parseInt(tSpan) to convert tSpan to integer

        var yyyy = today.getFullYear();

        var present = mm+'/'+dd+'/'+yyyy;

         $(".DateCalculated2 input").val(present); // the calendar date box
         $(".DateCalculated2 input").change(); 

  }

Suggestions?

0 0
replied on March 31, 2017

Ok. Finally found what was causing the miscalculation for the month. I was missing "setMonth" in the code

 

Here is the final code for anyone that might be trying something similar

 

      function recalSchedDate() { 
        
         var tSpan = $(this).val();   // from radio button
         $('.prefSched_box input').val(tSpan); // this will add then number of months (3 or 6) to today
         var prefSched_box = $('.prefSched_box input').val();

        var today = new Date();
        today.setMonth(today.getMonth() + parseInt(tSpan)); // I was missing this
        var dd = today.getDate();
        var mm = today.getMonth()+1; //January is 0! ---- // month witout leading zeros //---- use parseInt(tSpan) to convert tSpan to integer

        var yyyy = today.getFullYear();

        var present = mm+'/'+dd+'/'+yyyy;

         $(".DateCalculated2 input").val(present); // the calendar date box
         $(".DateCalculated2 input").change(); 

  }

 

0 0

Replies

You are not allowed to reply in this post.
You are not allowed to follow up in this post.

Sign in to reply to this post.