- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2025 02:54 AM
Hi,
I have 2 date variables, need to calculate the diffrence between 2 dates and if its less than 30 days need to dispay a help text, Any suggestion how to achieve this.
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2025 03:08 AM
you can use onChange client script on both the date variable and determine the days and then show help text
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// hide help text or field message
var startDate = new Date(g_form.getValue('variable1'));
var endDate = new Date(g_form.getValue('variable2'));
// Calculate the difference in time (milliseconds)
var timeDifference = endDate - startDate;
// Convert the time difference to days
var dayDifference = timeDifference / (1000 * 3600 * 24);
if (dayDifference < 30) {
// show help text or field message
}
}
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
01-10-2025 03:23 AM
seems issue because of date format
with which date format did you try?
I tried with YYYY-MM-DD and it gave me correct output as 9 days for 1st Jan and 10th Jan
try this updated script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// hide help text or field message
var startDate = new Date(getDateFromFormat(g_form.getValue('variable1'), g_user_date_format));
var endDate = new Date(getDateFromFormat(g_form.getValue('variable2'), g_user_date_format));
// Calculate the difference in time (milliseconds)
var timeDifference = endDate - startDate;
// Convert the time difference to days
var dayDifference = timeDifference / (1000 * 3600 * 24);
if (dayDifference < 30) {
// show help text or field message
}
}
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
01-10-2025 03:14 AM
Hi Ankur
im chosing 10 & 12 as the start and end dates but the diff is coming as 61
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2025 03:23 AM
seems issue because of date format
with which date format did you try?
I tried with YYYY-MM-DD and it gave me correct output as 9 days for 1st Jan and 10th Jan
try this updated script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// hide help text or field message
var startDate = new Date(getDateFromFormat(g_form.getValue('variable1'), g_user_date_format));
var endDate = new Date(getDateFromFormat(g_form.getValue('variable2'), g_user_date_format));
// Calculate the difference in time (milliseconds)
var timeDifference = endDate - startDate;
// Convert the time difference to days
var dayDifference = timeDifference / (1000 * 3600 * 24);
if (dayDifference < 30) {
// show help text or field message
}
}
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
01-10-2025 03:34 AM
Hi Ankur,
Thanks for the help im able to get the days diffrence.