Last active
December 2, 2023 07:08
-
-
Save kr4uzi/220bf8551c50aa552f3f889cc9aedfc5 to your computer and use it in GitHub Desktop.
ServiceNow Client Side Date Handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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