Skip to main content
patrickw12950595
Participant
June 13, 2017
Answered

Calculate end date from start date and number days to add

  • June 13, 2017
  • 2 replies
  • 1828 views

I'm trying to calculate a end date from a start date and number of days to add, both collected via user input. I've read the tutorials relevant and thought I understood how it worked but I can not figure out why my code wont output.

So user enters that the start date is 01/02/03 and it takes 5 days. I need the end date to output 01/07/03. I can't seem to see why it won't.

Here's what I've got:

-------------------------------------------------------------------------------------------------------------------

var startDate = this.getField("startDate_1").value; // get start date string

var endDate = this.getField("endDate_1"); // define the field the end date will be in

if(startDate.value != ""){

    var dateConverted = util.scand("mm/dd/yy", startDate); // convert string to date object

    var addDays = this.getField("days_1");

    var oneDay = 24 * 60 * 60 * 1000;

    var addDayMillis = addDays * oneDay;

    var endMillis = dateStart.getTime() + addDayMillis;

    var calcEndDate = new Date(endMillis);

    endDate.value = util.printd("mm d, yy",calcEndDate);

}

else{

    endDate.value = "----";

}

-------------------------------------------------------------------------------------------------------------------

What did I miss here?

This topic has been closed for replies.
Correct answer try67

Do you mean that instead of 5 days you want to add a number that comes from a field?

That code seems fine to me. What happened when you used it? Did you check the JS Console for errors?


I replied in your other thread: https://community.adobe.com/t5/acrobat/add-x-days-from-input-number-to-calendar-date-and-get-end-date/m-p/11753692?page=1#M293766

2 replies

Participant
January 14, 2021

Hi, did you get this solved?..  I'm trying to do the same thing but have no clue on how to get this work

try67
Community Expert
Community Expert
January 14, 2021

What have you tried?

Participant
January 14, 2021

the code up there.. and have this one where I can let a fixed number.. but want to be an input number.. 

 

var strStart = this.getField("DateStart").value;
if(strStart.length)
{
var dateStart = util.scand("mmm d, yyyy",strStart);
var oneDay = 24 * 60 * 60 * 1000;
var dueMillis = dateStart.getTime() + 5 * oneDay; 
var dueDate = new Date(dueMillis);
event.value = util.printd("mmm d, yyyy",dueDate);
}
else
event.value = "NA";

 

Bernd Alheit
Community Expert
Community Expert
June 13, 2017

Where did you define dateStart? Where do you use dateConverted?