- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 08:05 AM
Hi Team,
Currently trying to get the difference between two dates which return how many months and days are in the between them.
Currently using the below code
Output:
13 months 5 days
But actual difference is 13 months and 1 day
Please can anyone let me know what's the issue here, Thanks in advance.
Thank you
Prajwal
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 01:14 PM
try the below script.
// Create GlideDateTime objects for the two dates
var startDate = new GlideDateTime("2024-04-22");
var endDate = new GlideDateTime("2025-05-22");
// Calculate the difference in months and days
var monthsDifference = endDate.getMonth() - startDate.getMonth() +
(endDate.getYear() - startDate.getYear()) * 12;
if (endDate.getDayOfMonth() < startDate.getDayOfMonth()) {
monthsDifference--;
var daysDifference = new GlideDateTime(endDate).subtractMonths(monthsDifference).getDayOfMonth() - startDate.getDayOfMonth();
} else {
var daysDifference = endDate.getDayOfMonth() - startDate.getDayOfMonth();
}
// Log the result
gs.info("Months: " + monthsDifference + ", Days: " + daysDifference);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 01:14 PM
try the below script.
// Create GlideDateTime objects for the two dates
var startDate = new GlideDateTime("2024-04-22");
var endDate = new GlideDateTime("2025-05-22");
// Calculate the difference in months and days
var monthsDifference = endDate.getMonth() - startDate.getMonth() +
(endDate.getYear() - startDate.getYear()) * 12;
if (endDate.getDayOfMonth() < startDate.getDayOfMonth()) {
monthsDifference--;
var daysDifference = new GlideDateTime(endDate).subtractMonths(monthsDifference).getDayOfMonth() - startDate.getDayOfMonth();
} else {
var daysDifference = endDate.getDayOfMonth() - startDate.getDayOfMonth();
}
// Log the result
gs.info("Months: " + monthsDifference + ", Days: " + daysDifference);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2024 09:13 PM
Hi @VarunS ,
if we are giving end date[2025-05-11] lesser than start date getting an error subtractMonths is undefined.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 07:34 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 10:17 PM - edited 05-22-2024 10:17 PM
Hello @Prajwal G Vaish,
You can use below script as well to get dezired output.
*** Script: month of startDate 4
*** Script: Month of endDate: 5
*** Script: year of startDate: 2024
*** Script: year of endDate: 2025
*** Script: Difference of Days 395
*** Script: Months: 13, Days: 395
Please mark my answer as correct and helpful if your query is resolved/addressed.
Thank You,
Rajesh