Client Script Date Compare
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2017 06:24 AM
Just trying to use a client script to compare our start_date (DATE ONLY) field to the current date and if the start date is before the current date, then popup an alert. Problem is our start_date field only returns the date plus 00:00:00 as the time and the "new Date()" returns the current date plus the current time. So since our start_date time is 00:00:00 and the "new date()"
time is 09:17:42 it thinks the start_date is prior. How can I adjust this to look at the date only? or adjust the "new date()" time to always be 00:00:00? or format all dates to just be dates without time?
Here is the alert so I could just see the dates it was using:
Please mark this response as correct and/or helpful if it assisted you with your question.
Steven
- 20,834 Views

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2017 06:36 AM
You can use the javascript methods .getDate(), getMonth() and getFullYear() to pull out some specifics. Here's a stackoverflow link where someone is formatting a Date object.
How to format a JavaScript date - Stack Overflow
Cheers,
Dylan

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2017 06:42 AM
I thinks its also worth noting that it'd be easier using a before business rule since you have access to the GlideDateTime API. They have all kinds of methods where you can manipulate time objects. Instead of an alert you could use gs.addErrorMessage() and current.setAbortAction(true).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2017 06:48 AM
Hi,
You need to use GlideAjax to complete this requirement. Refer below scripts
Client script:
function onChange(control, oldValue, newValue, isLoading) {
if(newValue){
var ga = new GlideAjax('SvcCatalogCheckEndDate'); //Name of the Script Include
ga.addParam('sysparm_name', 'chkCatEndDate'); //Name of the function in the script include
ga.addParam('sysparm_date',g_form.getValue('start_date')); //Parameter to pass to the script include
ga.addParam('sysparm_endDate', g_form.getValue('end_date')); //Parameter to pass to the script include
ga.getXML(SvcCatalogCheckEndDateParse);
}
}
//Function that gets the response and will return to the client. You can place your alert in this function
function SvcCatalogCheckEndDateParse(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer == 'true'){
return;
}
if(g_form.getValue('end_date') != '' && answer == 'false'){
alert(getMessage("The End Date cannot be a date that is before the Planned Start Date"));
g_form.setValue('end_date', '');
}
}
Script include: Should be client callable
var SvcCatalogCheckEndDate = Class.create();
SvcCatalogCheckEndDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
chkCatEndDate : function() {
var start = this.getParameter('sysparm_date'); //Passing the start date from the client
var end = this.getParameter('sysparm_endDate'); //Passing the end date from the client
var dif = gs.dateDiff(start, end, true); //Get the Different between dates.
if (dif <= 0){
return false;
}
else
{
return true;
}
}
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2017 07:01 AM
HI Steven,
Using wrting the Date comparison on Client side is not a best practice
you can use the @Saprem code writing the script include and calling script include from client side.
Thanks
Chandu Telu