- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 03-03-2019 06:11 PM
Usage Scenario
I created a Date variable in a Catalog Item which represented the Date Required. I wanted to check that this data was at least 2 days in the future and NOT on a weekend.
Investigation (Googling)
Initially, I searched the communities for examples of how to Validate dates on the client side.
Many articles suggested that I use of AJAX to check the date on the server side. Suffering any latency for such a trivial task as setting dates seamed to be a bit silly. I finally came across a few examples of client side checking. Below is another example, for sharing:
Sample UI Client Script
Below is a sample UI Client Script which runs entirely on the client and validates a Variable of type Date in a Catalog Item. This script checks to see if the 'date_required' (i.e. NewValue) is >= 2 days in the future and not on a weekend.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//get the new and current date/time as an object
var dateObjectNow = new Date();
var dateObjectNew = new Date(newValue);
//get the dates in days - also use floor to convert valeus to integers
var dateNow = Math.floor(dateObjectNow.valueOf()/(1000*60*60*24));
var dateNew = Math.floor(dateObjectNew.valueOf()/(1000*60*60*24));
// Get day of week (Sunday = 0)
var dayOfWeek = dateObjectNew.getDay();
// Check Date if date is 2 or more days in the future and not on the weekend.
var msg;
if (dateNew >= (dateNow + 2) && dayOfWeek > 0 && dayOfWeek < 6) {
msg = 'Date is OK';
g_form.hideFieldMsg('date_required',true);
g_form.showFieldMsg('date_required',msg,'info',false);
}
else {
msg = 'ERROR: Date must be 2 or more days in the future and not on the weekend.';
g_form.hideFieldMsg('date_required',true);
g_form.showFieldMsg('date_required',msg,'error',false);
}
}
- 6,230 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
the main reason people suggest an AJAX call is because the newValue will always be in the user’s selected date/time format and perhaps the Date object won’t always get it right.
For example, could your script handle dates in MM/DD/YYYY, and DD/MM/YYYY?
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Our ServiceNow instance is based on Sydney and my Browser Client is located in NZ. We all use the international data format dd/mm/yyyy - so there is no issue.
Also, ServiceNow uses yyyy-mm-dd. See below.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
I did some further investigation about issues with dates:
The Date variable type in ServiceNow Catalog Items only accept sets dates in the format: yyyy-mm-dd. So there is no issues with having different International and US date formats. Good choice for date format in ServiceNow; Well done someone. Avoids these issues.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
@Jon BarnesI saw the scenario mentioned in your post. I ended up accounting for it with the code below. I statically set a time; however, the same logic used for capturing day, month, and year could also be written to capture hours ('HH'), minutes ('mm'), and seconds ('ss').