
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2016 04:42 AM
I have a date field where I want to ensure that the date selected from the date variable is not greater than the current days date. I have the following code for another date field that has to be 5 days out. Not very good with the date thing yet, so 1) should i have something like a (-1) in my dateMS portion of the script. And 2) will it render an error if the customer tries to select a date in the past or should I include some code that has an alert type of box stating something like "Please select a future date"......
var today = new Date();
var dateMS = today.getTime();
dateMS = dateMS + (5*24*60*60*1000);
var newDT = new Date();
newDT.setTime(dateMS);
g_form.setValue('ftr_needed_by',formatDate(newDT,g_user_date_format));
Thank you,
Karen
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-08-2016 12:35 PM
Karen,
Nothing wrong with the code, what happening is that 'getUTCDate() returns the day;
The reason that it did not allow you to select 2nd March is because 2 is lesser than 8 (current day) and conversely 16 (febr) is greater than 8 !!
I will suggest you to try with dateDiff() method to calculate the difference between two date/times given.
let me know if you have more concerns
Kind regards
Do not feel shy to mark correct or helpful answer if it helps or is correct

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2016 08:13 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2016 09:23 AM
Karen,
there was a pb with the row 17 and 19.
Here is the corrected script :
function verifyDates() {
var dReqStart = new GlideDateTime();
dReqStart.setDisplayValue(current.u_req_start_date);
var dReqEnd = new GlideDateTime();
dReqEnd.setDisplayValue(current.u_req_end_date);
var today = new GlideDateTime();
today.setDisplayValue(gs.now());
if ((dReqStart.getNumericValue() - today.getNumericValue())/(1000*60*60*24) < 0) {
gs.addErrorMessage("Request Start Date cannot begin before today.");
current.setAbortAction(true);
} else if ((dReqEnd.getNumericValue() - dReqStart.getNumericValue())/(1000*60*60*24) < 0) {
gs.addErrorMessage("Request End Date cannot come before Request Start Date.");
current.setAbortAction(true);
}
}
Kind regards,
ZA
Do not feel shy to mark correct or helpful answer if it helps or is correct

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-08-2016 04:39 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-08-2016 05:02 AM
Hi Karen, could you please try this structure:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// your logic goes here...
}
And please use g_form.getValue and g_form.setValue functions accordingly.
This is a wiki you can refer too Useful Catalog Scripts - ServiceNow Wiki
Hope this will help you.
BR,
/Suraj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-08-2016 05:08 AM
This is a script that i have used, may be modify accordingly to your purpose.
Hope this will help you.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if (!isLoading) {
if(newValue != '') {
//current date
var currentDateObj = new Date();
var currentDateStr = formatDate(currentDateObj, g_user_date_format);
var currentDateNum = getDateFromFormat(currentDateStr, g_user_date_format);
//get delivery_date
var startDateStr = g_form.getValue('estimated_delivery_date');
var startDateNum = getDateFromFormat(startDateStr, g_user_date_format);
if (startDateNum <= 0){
alert('Please use the calendar icon to select a date.');
g_form.setValue('estimated_delivery_date', '');
} else if (startDateNum < currentDateNum) {
alert('You cannot select a date in the past.');
g_form.setValue('estimated_delivery_date', '');
}
}
}
}
BR,
/Suraj