
- 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 04:45 AM
Karen,
Here you go for the script that checks the start date against the current date, and throws an error message if the start date is before the current date. It also checks the end date against the start date, and throws up an error message if the end date is before the current date. (you just have to change the field name on row 3 and row 5)
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-07-2016 05:08 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2016 05:54 AM
Hi Karen,
Write onChange Catalog Client Script on that particular 'Date' variable & make an ajax call. Here is the sample code snippet
//Catalog Client Script end
var ajax = new GlideAjax('<Client Callable Script_Include Name>');
ajax.addParam('sysparm_name','getNowDateTimeDiff');
ajax.addParam('sysparm_reqDate',newValue); //Pass selected date variable
ajax.getXMLWait(); //You can also go for asynchronous call
var result=ajax.getAnswer();
if(result<0) {
g_form.clearValue('date_variable'); //Clear Date Variable
alert('Please select a future date');
return false;
}
//Client Callable Script Include End
getNowDateTimeDiff: function(){
var reqDate = this.getParameter('sysparm_reqDate');
var diff = gs.dateDiff(gs.nowDateTime(),reqDate,true);
return diff;
},
Thanks
Abhinandan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2016 06:21 AM
Karen
It would be better to put it on 'onChange' CLient script.
Every time you will change the field value, it will check if the value entered is correct
Kind regards,
ZA
Do not feel shy to mark correct or helpful answer if it helps or is correct