AnsweredAssumed Answered

Date Calculations with javascript

Question asked by amcmahon on Aug 11, 2016
Latest reply on Aug 11, 2016 by amcmahon

Good morning everyone!

 

I am trying to use the method, described by Vadim, here, with one small change.  Instead of adding a static number to the date, I'd like to have a "Number of Days" Field, that would be filled out while filling out the form.

 

I've modified the code to add a "NumberOfDays" variable, pull in the number field I set in the form, and then put varNumberofDays in place of his 129, however I'm getting sporadic results.

 

I think I've narrowed it down to to this line, "var varNumberofDays = NWF$(# + varNumberOfDaysID);"


Can someone help.  My "new" code is pasted below.

 

 

 

NWF$(document).ready(function()
{
});function validateStoredDate(source, arguments)
{
  var varStoredDate = NWF$('#' + varStoredDateID);
  var varCalculatedDate = NWF$('#' + varCalculatedDateID); 
  var varNumberofDays = NWF$('#' + varNumberOfDaysID); 

 // 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() + varNumberofDays);  
 // 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[i])){
        date = theDate[i];
      }
      else if (/m/.test(theFormat[i])){
        month = parseInt(theDate[i], 10) - 1;
      }
      else if (/y/.test(theFormat[i])){
        year = theDate[i];
      }
    }
    return (new Date(year, month, date));
} 

Outcomes