The CreatorCon Call for Content is officially open! Get started here.

Get Months and days between two date

Prajwal G Vaish
Giga Guru

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

 

// Create GlideDateTime objects for the two dates
var startDate = new GlideDateTime("2024-04-22");
var endDate = new GlideDateTime("2025-05-22");

// Calculate the difference between the two dates
var duration = GlideDateTime.subtract(startDate,endDate); // This returns a GlideDuration object

// Get the difference in days
var days = duration.getRoundedDayPart();
gs.info(days);

// Calculate the number of months and days
var months = Math.floor(days / 30); // Approximate the number of months
days = days % 30; // Calculate the remaining days

// Log the result
gs.info("Months: " + months + ", Days: " + days);

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

1 ACCEPTED SOLUTION

VarunS
Kilo Sage

@Prajwal G Vaish 

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);

View solution in original post

4 REPLIES 4

VarunS
Kilo Sage

@Prajwal G Vaish 

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);

Hi @VarunS ,
if we are giving end date[2025-05-11] lesser than start date getting an error subtractMonths is undefined.


Prajwal G Vaish
Giga Guru

Hi @VarunS,

Thank you very much😊. It is working fine

 

Rajesh_Bhise
Tera Guru

Hello @Prajwal G Vaish,

 

You can use below script as well to get dezired output.

 

function formatNumber(number) {
    if (number > 9)
        return number;
    return "0" + number;
}

// 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 duration = new GlideDate.subtract(endDate, startDate);
    var milliseconds = new GlideDateTime(duration.getValue()).getNumericValue();
    var totalSeconds = milliseconds / 1000;
    totalSeconds = Math.abs(totalSeconds);
    var days = formatNumber(Math.floor(totalSeconds / 60 / 60 / 24));
    gs.info("month of startDate " + startDate.getMonth());
    gs.info("Month of endDate: " + endDate.getMonth());
    gs.info("year of startDate: " + startDate.getYear());
    gs.info("year of endDate: " + endDate.getYear());
    gs.info("Difference of Days " + days);
}
// Log the result
gs.info("Months: " + monthsDifference + ", Days: " + days);
 
*** 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