Calculate the amount of days between two dates

chrisreidr
Tera Contributor
Hey everyone, I am relatively new and am trying to determine how I can subtract two dates so that I ultimately end up with the number of days between the inputed value (date) and the current date.

I am returning the date values and current date appropriately but can't seem to figure out how to find the number of days between the two dates. Below are the dateDifference variables I tried using with no success.
 
What else can I try to calculate the value of days between the dateEntered and currentDate variables?
 
var gr = new GlideRecord('team_members');
    gr.query();
    while(gr.next()) {
        var dateEntered = gr.getValue('stage_date');
        var gdt = new GlideDateTime();
        var currentDate = gdt.getLocalDate();
        /*
          var dateDifference = Number(dateEntered) - Number(currentDate);
          var dateDifference = dateEntered.getNumericValue() - currentDate.getNumericValue();
          var dateDifference = GlideDateTime().subtract(dateEntered, currentDate);
          var dateDifference = GlideDateTime().subtract(dateEntered.displayValue(), currentDate.displayValue());
         */
        gs.info(dateDifference);
    }
 
 
2 ACCEPTED SOLUTIONS

Claude DAmico
Kilo Sage

This may entirely have to do with date and time formatting. Try something like what I have below.

 

//Strings must be converted to date/time format for getNumericValue()
//Dates must also be converted to have a time value
/*
var enteredDate = new GlideDateTime(gr.getValue('stage_date') + " 00:00:00"); //Assuming field is a string value in the format "01/23/2023"
var enteredDate = new GlideDateTime(gr.getValue('stage_date')); //Assuming field is a date only field
*/

var enteredDateValue = enteredDate.getNumericValue(); //This returns in milliseconds

//Getting the local date/time was done correctly
var gdt = new GlideDateTime();
var currDate = gdt.getLocalDate();

var dateDiff = enteredDateValue - currDate; //Still in milliseconds here
var daysDiff = dateDiff / 24 / 60 / 60 / 1000; //Convert to days. Use Math.abs(dateDiff) to avoid negative values if needed.

 

Claude E. D'Amico, III - CSA

View solution in original post

J_31
Kilo Sage

You can use the GlideDateTime object's getNumericValue() method. This method returns the number of milliseconds between two dates, which can be converted to days by dividing the result by the number of milliseconds in a day.

var gr = new GlideRecord('team_members');
gr.query();
while(gr.next()) {
var dateEntered = gr.getValue('stage_date');
var gdt = new GlideDateTime();
var currentDate = gdt.getLocalDate();
var dateDifferenceInMs = currentDate.getNumericValue() - GlideDateTime(dateEntered).getNumericValue();
var dateDifferenceInDays = Math.floor(dateDifferenceInMs / (1000 * 60 * 60 * 24));
gs.info(dateDifferenceInDays);
}
we first calculate the date difference in milliseconds by subtracting the getNumericValue() of the dateEntered variable from the getNumericValue() of the currentDate variable. Then, we convert this value to days by dividing it by the number of milliseconds in a day (1000 * 60 * 60 * 24) and rounding down to the nearest integer using Math.floor(). Finally, we log the result using gs.info().

 

 

View solution in original post

7 REPLIES 7

Claude DAmico
Kilo Sage

This may entirely have to do with date and time formatting. Try something like what I have below.

 

//Strings must be converted to date/time format for getNumericValue()
//Dates must also be converted to have a time value
/*
var enteredDate = new GlideDateTime(gr.getValue('stage_date') + " 00:00:00"); //Assuming field is a string value in the format "01/23/2023"
var enteredDate = new GlideDateTime(gr.getValue('stage_date')); //Assuming field is a date only field
*/

var enteredDateValue = enteredDate.getNumericValue(); //This returns in milliseconds

//Getting the local date/time was done correctly
var gdt = new GlideDateTime();
var currDate = gdt.getLocalDate();

var dateDiff = enteredDateValue - currDate; //Still in milliseconds here
var daysDiff = dateDiff / 24 / 60 / 60 / 1000; //Convert to days. Use Math.abs(dateDiff) to avoid negative values if needed.

 

Claude E. D'Amico, III - CSA

This worked! I was not aware that getNumericValue() worked on strings and not a date format but in retrospect makes sense. Thanks for the help.

J_31
Kilo Sage

You can use the GlideDateTime object's getNumericValue() method. This method returns the number of milliseconds between two dates, which can be converted to days by dividing the result by the number of milliseconds in a day.

var gr = new GlideRecord('team_members');
gr.query();
while(gr.next()) {
var dateEntered = gr.getValue('stage_date');
var gdt = new GlideDateTime();
var currentDate = gdt.getLocalDate();
var dateDifferenceInMs = currentDate.getNumericValue() - GlideDateTime(dateEntered).getNumericValue();
var dateDifferenceInDays = Math.floor(dateDifferenceInMs / (1000 * 60 * 60 * 24));
gs.info(dateDifferenceInDays);
}
we first calculate the date difference in milliseconds by subtracting the getNumericValue() of the dateEntered variable from the getNumericValue() of the currentDate variable. Then, we convert this value to days by dividing it by the number of milliseconds in a day (1000 * 60 * 60 * 24) and rounding down to the nearest integer using Math.floor(). Finally, we log the result using gs.info().

 

 

chrisreidr
Tera Contributor

Great solution! Thank you for the help, it worked perfectly.