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

This appears to have done the trick!   Thanks!


On the portal we get the following error


getDateFromFormat is not defined


A bit late to this discussion, but trying to manipulate dates client-side is fraught with problems, namely the date format and timezone are user-settable so (a) there's inconsistency in the way they're displayed, and (b) they may display different dates, depending upon timezone.



The safer method is to interrogate it at the database (GlideDate object, GlideRecord to obtain) then manipulate them server-side, sending information back using the scratchpad or AJAX ScriptInclude.