Skip to content

Instantly share code, notes, and snippets.

@kr4uzi
Last active December 2, 2023 07:08
Show Gist options
  • Select an option

  • Save kr4uzi/220bf8551c50aa552f3f889cc9aedfc5 to your computer and use it in GitHub Desktop.

Select an option

Save kr4uzi/220bf8551c50aa552f3f889cc9aedfc5 to your computer and use it in GitHub Desktop.
ServiceNow Client Side Date Handling
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var today = new Date();
today.setUTCHours(0, 0, 0, 0);
//
// ServiceNow Date -> JavaScript Date
//
// Note: Assuming 'my_field' is a Date Field!
// For Date Time fields, use g_user_date_time_format instead!
var timestamp = getDateFromFormat(g_form.getValue('my_field'), g_user_date_format);
var date = new Date(timestamp);
if (date < today) {
alert('Date cannot be in the past!');
}
//
// JavaScript Date -> ServiceNow Date
//
var snDate = formatDate(new Date(timestamp), g_user_date_format);
g_form.setValue('my_field', snDate);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment