How to validate date which is type of glide_date?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-19-2017 02:53 AM
Hi All,
Could you please help with validating date with type (glide_date) in ServiceNow? this field only contains date value and not the date and time format?
Script should validate the date automatically with respect to system/user date format.
Kindly help ASAP
thanks,
Pavan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-19-2017 03:07 AM
Hi Pavan,
To validate the input of all date/time fields, you can use the following in a validation script (System Definition > Validation Scripts). Because the date/time format is hard coded in this script, it must match your instance's date/time format. If your instance's date/time format changes, you must update your validation script.
Set the validation script's type to Date/Time. Then, with this validation script, if a user enters an incorrect format in a date/time field, they will receive an error message.
function validate(value) {
if(!value)
return true; // empty fields are still valid dates
// We "should" have the global date format defined always defined. but there's always that edge case...
if(typeof g_user_date_time_format !== 'undefined')
return isDate(value, g_user_date_time_format);
// if we don't have that defined, we can always try guessing
return parseDate(value) !== null;
}

Thanks,
Rajashekhar Mushke
Rising star : 2022 - 2024
Community Leader -2018
Connect me on LinkedIn : Rajashekhar Mushke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-19-2017 03:29 AM
Hi Rajesh,
Thanks for your reply
This will work for the fields of type date and time, but in case of type glide_date (which contains only date not time) it is not working
Could you please suggest if any other way?
Thanks,
Pavan

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-04-2023 10:22 AM - edited ‎08-04-2023 10:23 AM
Hello,
In case someone comes to this old thread, you can (now) use g_user_date_format if you are checking a date without time in Client Script :
return isDate(value, g_user_date_format) ;
Best regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-04-2023 11:07 AM
Hi @pavankumary ,
Hope you are doing great.
To validate a date field with the "glide_date" type in ServiceNow and ensure it adheres to the system/user date format, you can follow these steps:
- Create a Business Rule in ServiceNow that triggers when a record is inserted or updated. Step 2: In the Business Rule, use a script to validate the "glide_date" field.
- In the Business Rule, use a script to validate the "glide_date" field.
// Business Rule Name: Validate Glide Date Field
// Table: <Name of the table where the glide_date field is located>
// When: Before
(function executeRule(current, previous /*, g*/ ) {
// Get the user's preferred date format from the system properties
var userDateFormat = gs.getProperty('glide.sys.date_format');
// Get the date value from the glide_date field
var inputDate = current.glide_date_field; // Replace 'glide_date_field' with the actual field name
// Validate the date format
if (!isValidDateFormat(inputDate, userDateFormat)) {
// If the date format is invalid, set an error message on the glide_date field
current.glide_date_field.setError('Invalid date format. Please use the format: ' + userDateFormat);
}
})(current, previous);
// Function to validate date format
function isValidDateFormat(dateString, format) {
// Use ServiceNow's date parsing function to check if the date is in the correct format
var parsedDate = gs.dateGenerate(dateString, format);
return parsedDate.isValid();
}
Regards,
Riya Verma