planned_end_date value returned in Client Script differs from displayed value on Change Task form
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
Hi Team,
I am working on a Client Script on the Change Task table to make the field "Reason for Late Task Closure" mandatory when a task is closed after the Planned End Date.
The script is triggered on the State field change and compares the current date/time with the planned_end_date value.
On Change Client script (on state Field):
var CLOSED_STATE = "3";
var plannedEnd = g_form.getValue("planned_end_date");
if (!plannedEnd) {
g_form.setMandatory("u_reason_for_late_task_closure", false);
g_form.setDisplay("u_reason_for_late_task_closure", false);
return;
}
// Convert ServiceNow date (25/Jul/2025 18:00:00) to JavaScript Date
var planned = parseSNDate(plannedEnd);
var now = new Date();
// Debugging
//alert("State : " + newValue);
//alert("Planned : " + planned);
//alert("Now : " + now);
if (newValue == CLOSED_STATE && now.getTime() > planned.getTime()) {
//alert("Condition TRUE");
g_form.setDisplay("u_reason_for_late_task_closure", true);
g_form.setMandatory("u_reason_for_late_task_closure", true);
} else {
//alert("Condition FALSE");
g_form.setMandatory("u_reason_for_late_task_closure", false);
g_form.setDisplay("u_reason_for_late_task_closure", false);
}
}
// Function to convert 25/Jul/2025 18:00:00 to JavaScript Date
function parseSNDate(dateStr) {
var months = {
Jan: 0,
Feb: 1,
Mar: 2,
Apr: 3,
May: 4,
Jun: 5,
Jul: 6,
Aug: 7,
Sep: 8,
Oct: 9,
Nov: 10,
Dec: 11
};
var parts = dateStr.split(' ');
var d = parts[0].split('/');
var t = parts[1].split(':');
return new Date(
parseInt(d[2], 10), // Year
months[d[1]], // Month
parseInt(d[0], 10), // Day
parseInt(t[0], 10), // Hour
parseInt(t[1], 10), // Minute
parseInt(t[2], 10) // Second
);
Observation
I noticed that the value returned from:
is different from the value shown on the form.
Example:
Value displayed on form
Questions
- Why does g_form.getValue('planned_end_date') return a different value than what is displayed on the form?
- Is the returned value always in UTC/internal format while the form shows the user's timezone-adjusted value?
- What is the recommended approach for comparing a Date/Time field with the current date/time in a Client Script?
- Should g_form.getDisplayValue(), GlideDateTime, or another approach be used to ensure accurate time comparisons?
Please fix this code as per my requirement & provide the correct one.
Any guidance or best practices would be appreciated.
Thank you.
