- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2019 10:15 AM
I'm comparing date field on my custom app and using the following script -
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
//get the entered date string
var dtstring = g_form.getValue('u_date_begin');
//convert the string to a JavaScript Date/Time
var dt = new Date(dtstring);
//get the current date/time
var rightNow = new Date().valueOf();
//convert entered date to milliseconds
var dateToCheck = new Date(dtstring).valueOf();
alert("TODAY - " +rightNow);
alert("FIELD - " +dateToCheck);
//Compare the two numbers
if (dateToCheck < rightNow) {
alert('You cannot select a date in the past.');
g_form.setValue('u_date_begin','');
}
}
------------------
It works for the future dates. However, it gives me the error message if I add today's date,
My requirement is to compare only the date part of it and not the time. Any thoughts??
Thanks in advance!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2019 08:40 AM
Ankur - I'm using the client script given below for this requirement :
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var EventDateTime = newValue;
var dt = getDateFromFormat(EventDateTime, g_user_date_format);
var newdt = new Date(dt);
var EventDate = formatDate(newdt, 'yyyyMMdd');
var RightNow = new Date();
var RightNowDate = formatDate(RightNow, 'yyyyMMdd');
if (EventDate < RightNowDate) {
g_form.clearValue('u_date_expire');
g_form.showFieldMsg('u_date_expire', 'The Contract Date Expire must be after the current date', 'error', true);
}
}
This script is working well for me. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2019 02:33 AM
Hi Amrita,
Any update on this?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2019 08:40 AM
Ankur - I'm using the client script given below for this requirement :
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var EventDateTime = newValue;
var dt = getDateFromFormat(EventDateTime, g_user_date_format);
var newdt = new Date(dt);
var EventDate = formatDate(newdt, 'yyyyMMdd');
var RightNow = new Date();
var RightNowDate = formatDate(RightNow, 'yyyyMMdd');
if (EventDate < RightNowDate) {
g_form.clearValue('u_date_expire');
g_form.showFieldMsg('u_date_expire', 'The Contract Date Expire must be after the current date', 'error', true);
}
}
This script is working well for me. Thanks!