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

tiagomacul
Giga Sage

Como calcular diferença de datas em DIAS?

How to Calculate Date Differences in DAYS?

Cómo Calcular Diferencias de Fechas en DÍAS?

 

var dateOne = new Date(2019, 06, 21); //Year, Month, Date
var dateTwo = new Date(2019, 06, 22);

compareDates(dateOne , dateTwo)

function compareDates(dateOne , dateTwo)
{
       if (dateOne > dateTwo) {  
            gs.print("Date One is greater than Date Two.");  
        }else {  
            gs.print("Date Two is greater than Date One.");  
        }  
}

 

 

dateDiff / subtract

var gdt1 = GlideDateTime('2015-07-24 18:58:00');

var gdt2 = GlideDateTime('2015-07-24 19:01:00');

var duration2 = GlideDateTime.subtract(gdt1, gdt2);

 

 

Duration calculation between start and end date in a scoped application is not working By Adith Sanjay

var start = 28-05-2023 08:21:54';
var removeStartSpace = start.split(" ");
var startDate = removeStartSpace[0].split('-');
var finalStart = startDate[2]+"-"+startDate[1]+"-"+startDate[0]; gs.info(finalStart);
var end = '04-06-2023 08:21:54';
var removeEndSpace end.split(" ");
var endDate = removeEndSpace[0].split('-');
var finalEnd = endDate[2]+"-"+endDate[1]+"-"+endDate[0]; gs.info(finalEnd);
var startDay = this.getParameter('sysparm_start');
var endDay = this.getParameter('sysparm_end');
var start = new GlideDateTime (finalStart);
var end = new GlideDateTime(finalEnd);
var diff = GlideDateTime.subtract(start, end);
var days diff.getRoundedDayPart();
gs.info(days);

 

Calculate the amount of days between two dates by Claude DAmico

//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.

 

Duration Field KB0780039  / KB0754082

 (dataChange || current.business_duration.nil()) 
current.business_duration = gs.calDateDiff(opened, closed, false); 

 

Function field using datediff, task.due_date, and the current date By David Hepburn

tiagomacul_0-1751066641000.png

 

Know-More-Now-Logo.jpg

 

Was useful, please leave your feedback!

 

 

Participe, entre nas comunidades, acompanhem os posts:

 

Comments
david631
Giga Expert

Interesting! I've normally converted the dates to numeric integers using getNumericValue before comparing them. I didn't realize you could safely do the comparison with non-numeric values like this. Then again, I usually work with GlideDateTime objects. I'm not sure if your script would work with those.

Version history
Last update:
‎07-25-2025 06:50 AM
Updated by: