cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
eric_beeler
Nintex Newbie

Calculate Date based on another date

Jump to solution

Hello,

On the same form, I am trying to trying to populate the date field to show 2 days ahead of the current date for a "due date." I don't necessarily need both dates, current date and due date. I just need the due date which is 2 days after the current date but this is the only way I have found that may work. I have tried using the javascript and following the instructions from this site but for some reason it's not working for me: http://vadimtabakman.com/nintex-forms-date-calculations-with-javascript.aspx 

What I have tried so far is creating the Stored date column (using the date and time type with a default value of Today's Date). Adding a Date/Time field called StoredDate. Under Validation I have custom validation turned to Yes and the Custom validation function set to validateStoredDate. In the Advanced settings, the Store Client ID in JavaScript variable is set to Yes and the variable name set to varStoredDateID. I added the second Date/Time field called CalculatedDate. Under Advanced settings, I changed the Store Client ID in JavaScript variable to Yes and the variable name set to varCalculatedDateID. Then I copied the javascript and pasted it to the Custom JavaScript in the Settings to see if it would work for the +129 days but it doesn't.

In the picture I provided, the first date next to Required Date is today's date. The second one below it is the CalculatedDate which shows up blank. How can I get this to work? Thanks!

Reply
8 Replies
courtney_shelto
Nintex Newbie

Re: Calculate Date based on another date

Jump to solution

Hmm, you don't want the user to change this, right? You just want to show them what it's going to be? So how about this. You have a form with a date control that defaults to todays date (which you can hide if you'd like). Then you have a calculated value control that adds 2 days to the other value (which is todays date) and shows that in your "Required Date" spot, but it's not editable. Then, after submission, you have a workflow actually set your "Required Date" field and do a calculation for today + 2 days.

Reply
eric_beeler
Nintex Newbie

Re: Calculate Date based on another date

Jump to solution

Hi Courtney, thanks for the response.

I actually want the date field to pre-fill to be 2 days from the current date but I want them to be able to change the date field if they want to. The current date + 2 days should be what the form populates.

0 Kudos
Reply
courtney_shelto
Nintex Newbie

Re: Calculate Date based on another date

Jump to solution

Ok, then I think we're back to the Javascript option. What specifically about it wasn't working for you? Can you post your code and screenshots of the configs on the controls?

Reply
eric_beeler
Nintex Newbie

Re: Calculate Date based on another date

Jump to solution

Here is the code:

NWF$(document).ready(function()
{
 
});

function validateStoredDate(source, arguments)
{
  var varStoredDate = NWF$('#' + varStoredDateID);
  var varCalculatedDate = NWF$('#' + varCalculatedDateID);

  // get date from first date picker
  var depart = parseDate(varStoredDate.val(),'mm/dd/yy');

  // add 129 days to the retrieved date
  depart.setDate(depart.getDate() + 2);

  // update second date picker
  varCalculatedDate.datepicker('setDate',depart);

  arguments.IsValid = true;
}

function parseDate(dateString, userFormat) {
    var delimiter, theFormat, theDate, month, date, year;
    // Set default format if userFormat is not provided
    userFormat = userFormat || 'yyyy-mm-dd';

    // Find custom delimiter by excluding
    // month, day and year characters
    delimiter = /[^dmy]/.exec(userFormat)[0];

    // Create an array with month, day and year
    // so we know the format order by index
    theFormat = userFormat.split(delimiter);

    //Create an array of dateString.
    theDate = dateString.split(delimiter);
    for (var i = 0, len = theDate.length; i < len; i++){
      //assigning values for date, month and year based on theFormat array.
      if (/d/.test(theFormat)){
        date = theDate;
      }
      else if (/m/.test(theFormat)){
        month = parseInt(theDate, 10) - 1;
      }
      else if (/y/.test(theFormat)){
        year = theDate;
      }
    }
    return (new Date(year, month, date));
}

Reply
eric_beeler
Nintex Newbie

Re: Calculate Date based on another date

Jump to solution

Validate step:

Stored Date for JavaScript:

Calculated Date for JavaScript:

Stored Date settings:

Result:

The first date field is the Stored Date and the screen shot was from yesterday so the date is 6/29/17 on the form. The second date is the Calculated Date which shows blank.

Thanks!

Reply
wilkies0106aw
Nintex Newbie

Re: Calculate Date based on another date

Jump to solution

You could have a calculated value for your default current date+2. This control has a formula something like - formatDate(dateAddDays(Current Date,2),'dd/MM/yyyy') .  Add a rule to this control based on the ChaneTime control   i.e equals(ChangeTime,true).

Next to calculated value control have a yes/no control (tick box called ChangeTime).

 

On top of the calculated value control place a date/time control (MyAlternateDate - select your date) - hide this control if the Change time is not selected - equals(ChangeTime,false)  You could also add a validation rule that this date control is not allowed to be less than current date+2  (dateAddDays(Current Date,-2)    

Then have a form variable to with a rule something like If(equals(ChangeTime,false),MyFormDate,MyAlternateDate) .

This variable is connected to your SharePoint column. Add a formatdate to the formula of the variable if you want date only as per the default calculated value field etc.

Reply
Automation Master
Automation Master

Re: Calculate Date based on another date

Jump to solution

have a look on this example how to populate datetime control based on value of other datetime control

https://community.nintex.com/message/34952?commentID=34952#comment-34952 

you can add to the script date calculation that add up some days as per your requirement.

Reply
eric_beeler
Nintex Newbie

Re: Calculate Date based on another date

Jump to solution

I received some help on the JavaScript and got the dates to work. I needed to add validateStoredDate(); after the ready function. The first date is +2 days and the second one is today's date.

So my full script reads as followed:

NWF$(document).ready(function()
{
  validateStoredDate();
});

function validateStoredDate(source, arguments)
{
  var varStoredDate = NWF$('#' + varStoredDateID);
  var varCalculatedDate = NWF$('#' + varCalculatedDateID);

  // get date from first date picker
  var depart = parseDate(varStoredDate.val(),'mm/dd/yy');

  // add 129 days to the retrieved date
  depart.setDate(depart.getDate() + 2);

  // update second date picker
  varCalculatedDate.datepicker('setDate',depart);

  arguments.IsValid = true;
}

function parseDate(dateString, userFormat) {
    var delimiter, theFormat, theDate, month, date, year;
    // Set default format if userFormat is not provided
    userFormat = userFormat || 'yyyy-mm-dd';

    // Find custom delimiter by excluding
    // month, day and year characters
    delimiter = /[^dmy]/.exec(userFormat)[0];

    // Create an array with month, day and year
    // so we know the format order by index
    theFormat = userFormat.split(delimiter);

    //Create an array of dateString.
    theDate = dateString.split(delimiter);
    for (var i = 0, len = theDate.length; i < len; i++){
      //assigning values for date, month and year based on theFormat array.
      if (/d/.test(theFormat)){
        date = theDate;
      }
      else if (/m/.test(theFormat)){
        month = parseInt(theDate, 10) - 1;
      }
      else if (/y/.test(theFormat)){
        year = theDate;
      }
    }
    return (new Date(year, month, date));
}

Thanks for the help everyone!!!

View solution in original post

0 Kudos
Reply