Date Validation Script Fails for Users with 12-Hour Time Format
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
Hi All,
I have a Script Include that validates the Planned Start Date for CAB scheduling. The logic is intended to prevent users from selecting dates before the current/next Wednesday at 4:00 PM.
The validation works correctly for users whose date/time format is set to 24-hour format. However, users with a 12-hour format (AM/PM) experience unexpected behavior. For example:
- User selects a planned start date on Wednesday after 4:00 PM.
- g_form.getValue('start_date') returns a value such as 4:30 PM.
- The validation incorrectly blocks the submission and displays the error message.
- The same user can successfully submit on Thursday.
- Users with 24-hour format do not face this issue.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var plannedDate = g_form.getValue('start_date');
var cabRequired = g_form.getValue('cab_required');
var modelValue = g_form.getValue('chg_model');
if ((modelValue '6b339d828766a5107a5a54e83cbb35b8' || modelValue '7b993f872876b5107a4b54e39bcb3v7') && (cabRequired == 'true')) {
var DateGR = new GlideAjax('global.closed_state');
DateGR.addParam('sysparm_name', 'validatesatrt');
DateGR.addParam('sysparm_sys', plannedDate);
DateGR.getXML(callback);
}
function callback(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'true') {
alert(plannedDate + "The selected start date/time is not valid for the next available CAB, please adjust the 'Planned Start Date' after the end of the CAB meeting");
g_form.clearValue('start_date', 'true');
}
}
}Script include
validatesatrt: function() {
var sys = this.getParameter('sysparm_sys');
var wednesdaydate;
var date = new GlideDateTime();
var day = date.getDayOfWeekUTC();
var week = new GlideDateTime(sys);
if (date > week) {
return 'true';
} else {
switch (day) {
case 0:
date.addDaysUTC(3);
wednesdaydate = date.getDate() + " 16:00:00";
break;
case 1:
date.addDaysUTC(2);
wednesdaydate = date.getDate() + " 16:00:00";
break;
case 2:
date.addDaysUTC(1);
wednesdaydate = date.getDate();
wednesdaydate = wednesdaydate + " 16:00:00";
break;
case 3:
wednesdaydate = date.getDate();
wednesdaydate = wednesdaydate + " 16:00:00";
break;
case 4:
date.addDaysUTC(6);
wednesdaydate = date.getDate() + " 16:00:00";
break;
case 5:
date.addDaysUTC(5);
wednesdaydate = date.getDate() + " 16:00:00";
break;
case 6:
date.addDaysUTC(4);
wednesdaydate = date.getDate() + " 16:00:00";
break;
}
if (week < wednesdaydate) {
return 'true';
}
}
return 'false';
},
type: 'closed_state'
});
What would be the recommended approach to ensure consistent validation regardless of 12-hour or 24-hour user settings?
Thanks in advance for any guidance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
Hello @Pradip-1 ,
g_form.getValue('start_date') returns the display value in the user's format and timezone (4:30:00 PM) & .new GlideDateTime(sys) expects the internal format (yyyy-MM-dd HH:mm:ss)
So parse the client value with setDisplayValue() instead of the constructor, so the user's format and timezone are honored.
var selected = new GlideDateTime();
selected.setDisplayValue(this.getParameter('sysparm_sys'));
Also Other recommendations :
- getDayOfWeekUTC() returns (Mon - Sun), so case 0 never fires.
- clearValue() takes only the field name.
- Add a Business Rule or Data Policy that reuses the same Script Include logic, since a client script alone doesn't cover API, import or portal submissions.
If my response helped mark as helpful and accept the solution.
