- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-27-2018 10:41 PM
Hello team,
I have a requirement in change request if user selects the past planned date it should have to popup an alert message to choose the current or future date.
It is working fine for almost all users with my old script,
it fails when user choose the date format like mm/dd/yy in their profile
my new script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var startDateStr = g_form.getValue('start_date');
if(moment) {
var dateFormat = g_user_date_time_format;
var currentDate = new moment(g_form.getValue('start_date'),dateFormat);
alert('currentDate**' + currentDate + 'startDateStr**' +startDateStr);
if(startDateStr <= currentDate) {
alert('"Planned Start Date" must be set to a current or future time');
g_form.setValue('start_date', '');
}
}
}
Old script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var currentTime = '';
var time = new GlideAjax('DateTime');
time.addParam('sysparm_name', 'getNowDateTime');
time.getXML(
function () {
currentTime = time.getAnswer();
if (newValue <= currentTime){
alert('"Planned Start Date" must be set to a current or future time');
g_form.setValue('start_date','');
}
}
);
}
my issue is my new script not able to identify the current date , it shows like below,
How to achieve this?
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-27-2018 11:49 PM
Hi,
pass both newvalue and current value to script include and go comparison there and just return true or false to client script.
Convert both to GlideDateTime and then compare.
Thanks,
Ashutosh

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-28-2018 01:09 AM
Hi,
Try this script:
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var currentTime = '';
var time = new GlideAjax('DateTime');
time.addParam('sysparm_name', 'getNowDateTime');
time.addparam('sysparm_time',newValue);
time.getXML(
function () {
currentTime = time.getAnswer();
alert('newValue**' + newValue + 'currentTime**'+currentTime );
if (currentTime == 'true'){
alert('"Planned Start Date" must be set to a current or future time');
g_form.setValue('start_date','');
}
}
);
}
Script Include:
var DateTime = Class.create();
DateTime.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getNowDateTime: function () {
var firstDT = this.getParameter('sysparm_time'); //First Date-Time Field
var diff = gs.dateDiff(firstDT, gs.nowDateTime(), true);
var answer = '';
if (diff <= 0){
answer = 'true';
}
else {
answer = 'false';
}
return answer;
},
getNowRelativeDateTime: function () {
var relativeDateTime = new GlideDateTime(gs.nowDateTime());
relativeDateTime.addSeconds(300);
return relativeDateTime;
},
getNowRelativeFutureDateTime: function () {
var relativeDateTime = new GlideDateTime(gs.nowDateTime());
relativeDateTime.addSeconds(-300);
return relativeDateTime;
}
});
Thanks,
Ashutosh

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-27-2018 11:11 PM
Hi,
Also i use this function in below link and it helps us alot:
https://community.servicenow.com/community?id=community_question&sys_id=38c40be9dbd8dbc01dcaf3231f9619e3
Thanks,
Ashutosh