Client Script to account for date format?

ndt13
Giga Expert

I have two date fields (not date/time) on a form that are used for the start and end of an event.

I created a onLoad client script that calculates the difference between the start and end dates to return the number of days.   Additional fields will appear on the form depending on how many days the calculation spits out.   For example, if it is a 3 day difference, then 3 fields appear.   If it is a 2 day difference, 2 fields appear.   However, the client script does not work correctly for users in Europe because they set their formats to dd/MM/yyyy instead of the system default of yyyy-MM-dd.

Does anyone know how I can modify this script to look at the user's date format so that it will work properly for users with a date format different than that of the system?

If it helps, here is my current script:

function onLoad() {

  setNotes.call();

  //Type appropriate comment here, and begin script below

    try {

    //Urgency

    var work_start = g_form.getControl('work_start');

    work_start.onchange = setNotes;

      } catch (err) {}

  try {

    //Urgency

    var work_end = g_form.getControl('work_end');

    work_end.onchange = setNotes;

      } catch (err) {}

}

function setNotes() {

    //Hide note fields

  g_form.setVisible('u_day_2_notes', false);

  g_form.setVisible('u_day_3_notes', false);

  g_form.setVisible('u_day_4_notes', false);

  g_form.setVisible('u_day_5_notes', false);

  g_form.setVisible('u_day_6_notes', false);

  g_form.setVisible('u_day_7_notes', false);

  g_form.setVisible('u_day_8_notes', false);

  g_form.setVisible('u_day_9_notes', false);

  g_form.setVisible('u_day_10_notes', false);

  g_form.setVisible('u_day_11_notes', false);

  g_form.setVisible('u_day_12_notes', false);

  g_form.setVisible('u_day_13_notes', false);

  g_form.setVisible('u_day_14_notes', false);

  //Convert start and end fields to dates and find the difference

  var d1 = new Date (g_form.getValue('work_start'));

  var d2 = new Date (g_form.getValue('work_end'));

    var diff = ((d2.getTime()-d1.getTime())/86400000 + 1); //the difference will always be at least 1

  //Show note fields based on the difference between the start and end dates

  if (diff >= 14) {

  g_form.setVisible('u_day_1_notes', true);

  g_form.setVisible('u_day_2_notes', true);

  g_form.setVisible('u_day_3_notes', true);

  g_form.setVisible('u_day_4_notes', true);

  g_form.setVisible('u_day_5_notes', true);

  g_form.setVisible('u_day_6_notes', true);

  g_form.setVisible('u_day_7_notes', true);

  g_form.setVisible('u_day_8_notes', true);

  g_form.setVisible('u_day_9_notes', true);

  g_form.setVisible('u_day_10_notes', true);

  g_form.setVisible('u_day_11_notes', true);

  g_form.setVisible('u_day_12_notes', true);

  g_form.setVisible('u_day_13_notes', true);

  g_form.setVisible('u_day_14_notes', true);

  } else {

  switch (diff) {

  case 13:

  g_form.setVisible('u_day_13_notes', true);

  case 12:

  g_form.setVisible('u_day_12_notes', true);

  case 11:

  g_form.setVisible('u_day_11_notes', true);

  case 10:

  g_form.setVisible('u_day_10_notes', true);

  case 9:

  g_form.setVisible('u_day_9_notes', true);

  case 8:

  g_form.setVisible('u_day_8_notes', true);

  case 7:

  g_form.setVisible('u_day_7_notes', true);

  case 6:

  g_form.setVisible('u_day_6_notes', true);

  case 5:

  g_form.setVisible('u_day_5_notes', true);

  case 4:

  g_form.setVisible('u_day_4_notes', true);

  case 3:

  g_form.setVisible('u_day_3_notes', true);

  case 2:

  g_form.setVisible('u_day_2_notes', true);

  default:

  g_form.setVisible('u_day_1_notes', true);

  break;

  }

  }

}

1 ACCEPTED SOLUTION

HV1
Mega Guru

"g_user_date_format" should work for date fields.   Try below code:



var strStartDt = g_form.getValue('work_start');  


var intStartDt = getDateFromFormat(strStartDt, g_user_date_format);              


var strEndDt = g_form.getValue('work_end');;  


var intEndDt = getDateFromFormat(strEndDt, g_user_date_format);    


var diff = (intEndDt - intStartDt)/86400000;  


alert("No. of Days Diff: " + diff);



Regards,


Hardik Vora


View solution in original post

7 REPLIES 7

Mike Allen
Mega Sage

Comparing Client Dates



In this blog, there is



var format = g_user_date_time_format;



Maybe you can use that to spark any ideas you can.   I am sorry I do not know the answer exactly, but this is something I worked with today and thought it might help.


I think this only works for date/time fields.   My start and end fields are date only.



I keep receiving this error message with these date fields:



onChange script error: TypeError: Cannot read property 'split' of undefined function (){var o=i(m,arguments);return l.apply(n,o)}



This does not appear when I try it with date/time fields.


That error is that you typically aren't working on a String variable type.   You can force it by doing something like:



var start = g_form.getValue("start_date") + '';  


  • var pd = parseDate(start.split(" ")[0]);

This example of g_form.getValue() should have returned a String anyway, but if "start_date" wasn't on the form then it would also cause your issue with .split() not being defined.



-Chris


HV1
Mega Guru

"g_user_date_format" should work for date fields.   Try below code:



var strStartDt = g_form.getValue('work_start');  


var intStartDt = getDateFromFormat(strStartDt, g_user_date_format);              


var strEndDt = g_form.getValue('work_end');;  


var intEndDt = getDateFromFormat(strEndDt, g_user_date_format);    


var diff = (intEndDt - intStartDt)/86400000;  


alert("No. of Days Diff: " + diff);



Regards,


Hardik Vora