Skip to main content
Participant
January 14, 2021
Answered

Add x days from input number to calendar date.. and get end date

  • January 14, 2021
  • 1 reply
  • 2820 views

I had this form fields.. 

One with the date Mar 4, 2021 and I want to add in this case 10 days but I want this be a free input and been able to put any number and at the want automatically get the date, in this case Mar 14, 2021.. and I'm not at all good a JS.. 

 

So I found a code where I cand add a X number but will be just that number.. how can I change that o get to work this formulas.. 

 

Attached a picture with forms and code that need to fix/change..

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;   <--- in this line the +5 are the day added, but

var dueDate = new Date(dueMillis);                                    w ant this be an input number..
event.value = util.printd("mmm d, yyyy",dueDate);
}
else
event.value = "NA";

 

 

 

This topic has been closed for replies.
Correct answer try67

Change that line to:

var dueMillis = dateStart.getTime() + (Number(this.getField("NumberOfDays").valueAsString) * oneDay);

 

(change "NumberOfDays" to the actual field name, of course)

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
January 14, 2021

Change that line to:

var dueMillis = dateStart.getTime() + (Number(this.getField("NumberOfDays").valueAsString) * oneDay);

 

(change "NumberOfDays" to the actual field name, of course)

Participant
January 27, 2023

Hey, I used this code and it worked. However, I am wishing to include the start date as day one. For example, Start date 01 Jan 23 - Days 5 - End date 05 Jan 23. 

What do I need to add? 

Thanks

Participant
January 27, 2023

Add 4 days.


Thanks for your reply Bernd. 

Apologies, I may not have explained my problem well enough. 

The number of days changes. Therefore, the start date and end date are inclusive. 

For example, the start date might be 01 Jan 23, the number of days could be anything from 01-500 - the end date needs to reflect each day as one day counted. 

The current script I am using - as stated in this thread does not count each day was one whole day. 

For example it my start date is 01 Jan, number of days 5, end date is then showing 06 Jan. I need it to show 05 Jan. 

 

What do I need to change to the script for this to occurr? 

Thanks in advance.