Copying the DATE portion of a datetime control into the DATE portion of another datetime control

  • 18 November 2015
  • 1 reply
  • 4 views

Badge +4

I'm having some difficulty grasping how datetime values from Nintex are stored/manipulated as javascript variables.  I'll begin by explaining what I'm trying to accomplish, as it seems it should be pretty easy.  I'm not very experienced with javascript but I've tried reworking multiple suggestions on these forums to fit my problem and I continue to run into issues.

I am working on a form to request parking validation.  There are two controls on the form that I'm concerned with, an arrival datetime and departure datetime.  What I'd like to do is when the DATE of one of these controls is changed, I'd like for that date to copy into the other control.  The time portion should not change.  Additionally, I want to be able to calculate the difference between these two times via a calculated field, which is currently working properly when the dates are manually selected from the datepicker.

The code snippet below is the closest I've been able to get.  When the arrival time is changed, the empty date constructor sets dateEntered to the current date, and the current date properly copies into the second control on the form.  As desired, the Time fields do not appear to be affected.  However, I cannot figure out how to create the date object with the value from the arrival time control so it behaves in a similar fashion.  When I try adding additional lines of code to store the date, the value stops copying altogether, so I'm having difficulty determining where the breakdown is and if it's a syntax error or an issue with how the date is formatted.

// Set up an on-change event for the Departing date box to copy the date value to Returning
var dateEntered = new Date();
var inpArriving = NWF$("#" + v_arrTime);

inpArriving.change(function()
{
  //var dateEntered = NWF$("#" + v_arrTime).val();
  NWF$("#" + v_depTime).val((dateEntered.getMonth()+1) + "/" + dateEntered.getDate() + "/" + dateEntered.getFullYear());
  alert("TEST");
});

In the above code, if I swap the comments on the var dateEntered assignments, the alert does not come through.  I've seen the same operation in numerous posts so I don't believe this is a syntax error but is more likely an issue with my lack of understanding regarding how the dates are stored and how they need to be manipulated to properly populate the control on the form.

Could anyone help me to understand what I need to do to implement this on-change function and achieve the desired results?  Or if you have a more simple solution/approach you'd like to suggest, please let me know.  I'm open to suggestions happy.png  Thanks!!


1 reply

Badge +4

I've been struggling with this for a little while now, but finally found a solution by combining a few other solutions I found online.  I'm including it below in case someone else comes across the same issue.

The links to the original solutions are below if you need more info.  The Nintex calculated value properly on my form calculates the difference between the two Nintex DateTime controls (or displays N/A if invalid), I'll include the code for that as well.

Setting current date and time on control

Comparing dates with javascript

Custom JavaScript for Form:

// Set up an on-change event for the Departing date box to copy the date value to Returning

var dateEntered = new Date();

var inpArriving = NWF$("#" + v_arrTime);

inpArriving.change(function()

{

  var inputdate = NWF$('#' + v_arrTime).val();

  var iDay = inputdate.slice(0,2)

  var iMonth = inputdate.slice(3,5)

  var iYear = inputdate.slice(6,10)

  NWF$("#" + v_depTime).val(iDay + "/" + iMonth + "/" + iYear);

});

Formula for Nintex Calculated field (to calculate and display difference (in hours and minutes) between the arrival time and departure time):

If(DepartureTime>ArrivalTime,dateDiffHours(ArrivalTime,DepartureTime)+"h "+(dateDiffMinutes(ArrivalTime,DepartureTime)%60)+"m","N/A")

Please let me know if you have any questions.  Thanks! laugh.png

Reply