23

I know that you can use daysBetween() for two Date objects, but what would I use to calculate the elapse time in seconds between two DateTime objects?

3 Answers 3

43

You can use the getTime method to get the milliseconds between them and then convert to whatever unit you need:

Long dt1Long = DateTime.now().addDays(-1).getTime();
Long dt2Long = DateTime.now().getTime();
Long milliseconds = dt2Long - dt1Long;
Long seconds = milliseconds / 1000;
Long minutes = seconds / 60;
Long hours = minutes / 60;
Long days = hours / 24;
1
  • 1
    No reason to create manual functions for this, getTime() is exactly what you want; from the docs: > Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this DateTime object. Commented May 30, 2019 at 17:27
9

I know this is kind of late, but I had a similar need for elapsed time which may help in your case.

So I created the following utility:

public static Time GetElapsedTime(Time startTime, Time endTime)
{
    if(startTime == null || endTime == null)
        return Time.newInstance(0, 0, 0, 0);

    Integer elapsedHours = endTime.hour() - startTime.hour();
    Integer elapsedMinutes = endTime.minute() - startTime.minute();
    Integer elapsedSeconds = endTime.second() - startTime.second();
    Integer elapsedMiliseconds = endTime.millisecond() - startTime.millisecond();

    return Time.newInstance(elapsedHours, elapsedMinutes, elapsedSeconds, elapsedMiliseconds);
}

Now that I have an elapsed time method, I could get my elapsed time as a Time object and do whatever I see fit. So for example, if you needed seconds elapsed for two DateTimes, you could use another method on-top of the one I put above.

So you can create another method like so:

public static Integer GetSecondsElapsed(Time startTime, Time endTime)
{
    return GetElapsedTime(startTime, endTime).second();
}

You can then call the method with your DateTimes like:

Integer secondsElapsed = YourUtilityClass.GetSecondsElapsed(YourDateTimeOne.time(), YourDateTimeTwo.time());

You could go a step further and do the following as well:

public static Time GetElapsedTime(DateTime startDate, DateTime endDate)
{
     if(startDate == null || endDate == null)
         return Time.newInstance(0, 0, 0, 0);
     return GetElapsedTime(startDate.time(), endDate.time());
}

Then have the following method:

public static Integer GetSecondsElapsed(DateTime startDate, DateTime endDate)
{
    return GetElapsedTime(startDate, endDate).seconds();
}

Now your utility will look something like this:

Integer secondsElapsed = YourUtilityClass.GetSecondsElapsed(YourDateTimeOne, YourDateTimeTwo));

This will now give you the flexibility you need. I needed the elapsed time in a readable format, so simply getting the string value of the time elapsed was suitable for me.

2

Building on Programmable Medley's solution for calculating elapsed time between two times. I found that this would not work for me:

return GetElapsedTime(startTime, endTime).second();

So because I actually wanted this I just changed it slightly. getElapsedTimeSeconds(startTime, endTime) method.

public static Integer GetElapsedSeconds(Time startTime, Time endTime)
    {
    // Save CPU. 
    if(startTime == null || endTime == null || startTime == endTime){
        return 0;
    }
    else{
        Integer elapsedHours = endTime.hour() - startTime.hour();
        Integer elapsedMinutes = endTime.minute() - startTime.minute();
        Integer elapsedSeconds = endTime.second() - startTime.second();
        Integer elapsedMiliseconds = endTime.millisecond() - startTime.millisecond();

        Integer total = ((elapsedHours * 60 * 60) +
                         (elapsedMinutes * 60) +
                         (elapsedMiliseconds / 1000) + 
                          elapsedSeconds );

        // Don't return negative values.
        if(total < 0){
            return 0;
        }else{
            return total;
        }
    }
}

Thanks for the starting place @ProgrammableMedley. Hope this helps someone.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .