
- 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-08-2016 06:52 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-08-2016 07:12 AM
Karen Brown
I See what the problem is.. Of course I will not work. What I suggest is to change from 'Client script' to business rules. I've just noticed that GlideDateTime does not work on client script as well 😕
I did not know that and also My apologize for not having tested it before
Create a new business rules,
Select the table name (i.e incident if it should run on incident form)
Update should be true,
advanced should be true
Business rule should run on Before
condition : ftr_needed_by changes
script :
(function executeRule(current, previous /*null when async*/) {
var dReqStart = new GlideDateTime();
dReqStart.setDisplayValue(current.ftr_needed_by); // make sure please that the field name is 'ftr_needed_by'
var today = new GlideDateTime();
today.setDisplayValue(gs.now());
if ((dReqStart.getNumericValue() - today.getNumericValue())/(1000*60*60*24) < 0) {
gs.addErrorMessage("ftr_needed_by Date cannot begin before today.");
current.setAbortAction(true);
}
})(current, previous);
This business rules will prevent to update the form. It will test the date populated
This time we definitely got it
One note :
GlideDateTime API instantiates a new GlideDateTime object from the current date and time.
Hope it helps
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 09:22 AM
AKb thank you for your assistance. I used the following script which works perfectly.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var today = new Date();
var start = new Date(g_form.getValue('Needed_by_date'));
g_form.hideFieldMsg('Needed_by_date');
if (start.getUTCDate() < today.getUTCDate())
{
g_form.showFieldMsg('Needed_by_date','Start date must be in the future.','error');
g_form.clearValue('Needed_by_date');
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-08-2016 07:19 AM
Dont use current.setAbortAction(true) in client script it is only Server side script.
just remove it and put return - that would be enough.
In order to verify dates, it always best to use onSubmit script because when you use onChange script you have to make the field mandatory and clear the value populated in the field - if not the user can still be able to submit the form with the incorrect date.
And in the showFieldMsg function, please add second argument to say that it is error
If its not resolved still, please create an onSubmit script and try this -
function onSubmit() {
g_form.hideFieldMsg('u_req_start_date');
var pattern = /\d\d\d\d-\d\d-\d\d (\d?\d):\d\d:\d\d/;
var value = g_form.getValue('u_req_start_date');
var verifyResult = pattern.exec(value);
if (verifyResult == null || verifyResult[0] != value) {
g_form.showFieldMsg('u_req_start_date','Start date must be in the YYYY-MM-DD HH:MM:SS date format','error');
return false;
}
else {
var startDateString = g_form.getValue('u_req_start_date');
var dateArray = startDateString.split(' ');
var dayArray = dateArray[0].split('-');
var timeArray = dateArray[1].split(':');
var startDate = new Date(dayArray[0], dayArray[1]-1, dayArray[2], timeArray[0], timeArray[1], timeArray[2]);
var now = new Date();
if (now > startDate) {
g_form.showFieldMsg('u_req_start_date','Start date can not be in the past','error');
return false;
}
}
return true;
}
If solved, mark the answer as correct

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-08-2016 09:22 AM
Thank you I have an onChange script that works:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var today = new Date();
var start = new Date(g_form.getValue('Needed_by_date'));
g_form.hideFieldMsg('Needed_by_date');
if (start.getUTCDate() < today.getUTCDate())
{
g_form.showFieldMsg('Needed_by_date','Start date must be in the future.','error');
g_form.clearValue('Needed_by_date');
}
}
Thank you for your assistance.
Karen