Date Calculations with javascript

  • 11 August 2016
  • 6 replies
  • 2 views

Badge +3

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)){

        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));

}


6 replies

Userlevel 5
Badge +14

what you mean with sporadic results?

do you mean than you get some error?

or don't you just get read value from form control into javascript local variable?

Badge +3

If I replace "var varNumberofDays = NWF$('#' + varNumberofDaysID);

with "var varNumberofDays = 30;

it works. (obviously it always just adds 30 to the start date)

However if I do it the other way, It just populated the "End Date" field with today's date.

Userlevel 5
Badge +14

what's type of 'number of days' control, single line of text?

then you read its value like

var varNumberofDays = NWF$(# + varNumberOfDaysID).val();

Badge +3

It is a "single line of text" set as an integer.

When I put your recommended change in, I get some weird results.

So here's the scenario.

Start date = 8/1/2016

If I put Number of days = 1, it sets the end date to 8/11/2016,

If I put Number of days = 2, it sets the end date to 8/12/2016 and so on.

If I put Number of days equal to 10, it sets the end date to 11/18/2016.

Feels like I'm a bit off, somewhere.

Thanks for all of your help.

Userlevel 5
Badge +14

looks like despite you set datatype to integer javascript interprets it as string.

try to use

var varNumberofDays = Number(NWF$('#' + varNumberOfDaysID).val());  

Badge +3

This was it! Thank you so much!

I was wondering how to make sure a variable was a number. Thanks so much!! 

Reply