- 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 02:58 AM
Hi @abjaffrey you can try client script like below.
var one_day=1000*60*60*24;
var start = g_form.getValue('request_datetime').split(' ')[0];
var end = g_form.getValue('launch_date');
var date1_ms = new Date(start);
var date2_ms = new Date(end);
var difference_ms = date2_ms - date1_ms;
// Convert back to days and return
alert("Difference in days");
alert(Math.round(difference_ms/one_day));
Please mark my answer correct and helpful if this works for you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2025 03:08 AM
hi,
i chose 10/01/25 & 12/01/25 as dates, the diffrence is coming as 61
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2025 03:05 AM
should be an easy task
this link has solution, please enhance
Calculate Number of Days between two dates(Start and End 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
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