- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-26-2015 03:10 AM
I have a catalog client script that is meant to be checking that the date selected is not in the past.
However, the message is displaying at all times - even if the date is in the future.
Script...
function onChange(control, oldValue, newValue, isLoading){
//Clear messages
g_form.hideFieldMsg('loan_required_from');
GlideUI.get().clearOutputMessages();
var msg;
//Get current date
var currentDateObj = new Date();
var currentDateStr = formatDate(currentDateObj, g_user_date_time_format);
var currentDateNum = getDateFromFormat(currentDateStr, g_user_date_time_format);
//Get start date
var startDateStr = g_form.getValue('loan_required_from');
var startDateNum = getDateFromFormat(startDateStr, g_user_date_time_format);
//Exits if date is empty
if (startDateStr == "") {
return;
}
if (startDateNum < currentDateNum) {
msg = 'Required from date/time cannot be in the past ';
alert(msg);
return false;
}
}
Solved! Go to Solution.
- Labels:
-
User Interface (UI)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-26-2015 11:45 AM
Hi all, this is now working using the below...
function onChange(control, oldValue, newValue, isLoading){
if (isLoading || newValue == '') {
return;
}
if(newValue != '')
{
var reqDate = new Date(getDateFromFormat(newValue, g_user_date_format));
var today = new Date();
if(reqDate.getTime() <= today.getTime()) {
alert('The loan required from date cannot be in the past');
g_form.setValue('loan_required_from','');
}
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-26-2015 03:55 AM
always do the date time comparison in the server side script is my suggestion .. Using date() tends to use your computer date time setting rather than your instance's date...
this could help ... Create a script include and add this to a functon
var futdate = new GlideDateTime();
var start = new GlideDateTime(this.getParameter('sysparm_date'));
if(start < futdate)
{
return false;
}
else
{
return true;
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-26-2015 04:42 AM
Hi Nat,
Please try below code.
var currentDateObj = new Date();
var currentDateStr = formatDate(currentDateObj, g_user_date_time_format);
var currentDateNum = getDateFromFormat(currentDateStr, g_user_date_time_format);
//Get start date
var startDateStr = g_form.getValue('loan_required_from');
var startDateNum = getDateFromFormat(startDateStr, g_user_date_time_format);
//Exits if date is empty
if (startDateStr == "") {
return;
}
var isEndBeforeStart = compareDates(currentDateStr, g_user_date_time_format, startDateStr, g_user_date_time_format);
if (isEndBeforeStart == 0) {
msg = 'Required from date/time cannot be in the past ';
alert(msg);
return false;
} else {
return true;
}
Below is the article you may want to refer to
Kalaiarasan P I think GlideDateTime is not available in client side scripting.
Regards,
Rajnish Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-26-2015 11:45 AM
Hi all, this is now working using the below...
function onChange(control, oldValue, newValue, isLoading){
if (isLoading || newValue == '') {
return;
}
if(newValue != '')
{
var reqDate = new Date(getDateFromFormat(newValue, g_user_date_format));
var today = new Date();
if(reqDate.getTime() <= today.getTime()) {
alert('The loan required from date cannot be in the past');
g_form.setValue('loan_required_from','');
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-26-2015 12:24 PM
I am trying to do something similar , but I want the date to be selected within one year or less. How would that script look like?