Expand my Community achievements bar.

Applications for the 2024-2025 Adobe Experience Manager Champion Program are open!

Newbie - Need help with date script

Avatar

Level 1

Hello all,

I have a form that has a From Date and a To Date.  I want the user to enter the From Date and the the form will populate the To date by adding 7 days to the From date.  Can anybody tell me how to do this?

Thanks in advance,

Gus

3 Replies

Avatar

Level 10

Hi Gus,

In the calculate event of your To date you could have the following JavaScript;

var msPerDay = 86400000;
function addDays(date, days)
{
      return new Date(date.valueOf() + (days * msPerDay));
}

if (From.rawValue != null)
{
      this.rawValue = util.printd('yyyy-mm-dd', addDays(util.scand('yyyy-mm-dd', From.rawValue), 7));
}

You don't have to worry about going off the end of a month as JavaScript just starts on the next month.

Note: you might have to change the date formats to suit your fields

Bruce

Avatar

Level 1

Thanks Bruce.  I don't know what I'm doing wrong but I can't get it to execute.  When I type the From Date, nothing happens.  The name of my date fields are FromDate and ToDate.  Am I missing something?

Please advise and thanks in advance.

Gus

Avatar

Level 10

Hi Gus,

One of the many frustrating things with Designer is JavaScript errors in Calculate events just seem to crash silently.  But I do think the calculate event is the right place in this case.

I've attached a form, using FromDate and ToDate field names.

Bruce