Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

planned_end_date value returned in Client Script differs from displayed value on Change Task form

Sirri
Kilo Sage

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:

g_form.getValue('planned_end_date')



is different from the value shown on the form.

Example:

Value displayed on form

11/Sep/2026 08:20:19

Value returned in script/XML
2026-09-11 05:20:19

The instance timezone is configured as:
Asia/Riyadh (UTC+3)

Because of this difference, the date/time comparison is not behaving as expected, and the mandatory field validation is firing inconsistently.

Questions

  1. Why does g_form.getValue('planned_end_date') return a different value than what is displayed on the form?
  2. Is the returned value always in UTC/internal format while the form shows the user's timezone-adjusted value?
  3. What is the recommended approach for comparing a Date/Time field with the current date/time in a Client Script?
  4. 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.




0 REPLIES 0