Validate date and time on a due date field that the entered value is not in the Past
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2023 01:21 AM
Hi Team,
I have a requirement to validate the entered date and Time is not in the past on the incident due date field.
I've tried to achieve this via Client Script and a Script Include.
The onChange Client script I have written on change of due date:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2023 07:05 AM
try this once
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if(g_form.getValue('due_date') != ''){
var nowTime = new Date().getTime();
var dueDate = new Date(getDateFromFormat(g_form.getValue('due_date'), g_user_date_time_format)).getTime();
if(dueDate < nowTime){
g_form.addErrorMessage('Please ensure that the Due date is in the future');
g_form.clearValue('due_date');
g_form.setMandatory('due_date');
}
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2023 05:12 AM
Hi Ankur,
I've tried the above solution, it is validating if I'm entering the past time, however if I enter a time in future, say 2 hours from the current time but on the same date, it still throws up an error.
I have attached the screen shot below which gave me the info message.
Scenario 1: as expected generated error on entering past time
Scenario 2: Logs shown when entering future dates.
what I observe from the both the scenarios is that the time remains same no matter what time is entered.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2023 04:58 AM
Hi Ankur,
I've tried the above solution, it is validating if I'm entering the past time, however if I enter a time in future, say 2 hours from the current time but on the same date, it still throws up an error.
I have attached the screen shot below which gave me the info message.
Scenario 1: as expected generated error on entering past time
Scenario 2: Logs shown when entering future dates.
what I observe from the both the scenarios is that the time remains same no matter what time is entered.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2023 05:47 AM - edited 08-23-2023 05:53 AM
Use gs.DateDiff();
You need to call script include in your client script with below code
var gdt1 = GlideDateTime();
var due_date = this.getParameter('sys_due_date');//this is due date field from client script
var duration1 = gs.dateDiff(gdt1.getDisplayValue(),due_date.getDisplayValue(), true);
return duration;//from script include
if(answer<=0){
g_form.addInfoMessage('Date should be future date');
}
If gdt1>gdt2 -this condition becomes true when due date is past date((duration1 returns negative value))
gdt1==gdt2 -when both dates are same (duration1 returns value-0)
gdt1<gdt2-when your due date is future value(duration1 returns positive value)