  # Calculate the Time Between Two Dates

Did you know you can calculate the time between two dates with Podio even if you're using different units of time? Here's how to use this flexible, handy tool:

if ( @Date1 == 0) return 0;
var a = moment( @Date1 );
var b = moment( @Date2 );
var res = b.diff(a, 'days');
if (res < 0) { 0 } else { res};

Here’s the breakdown of what this all means:

On the 4th line where it says: “days” you can switch that out with any measurement of time:

• milliseconds
• seconds
• minutes
• hours
• days
• weeks
• months
• years

It will automatically output the time between the into this unit!

Calculating only weekdays is a bit trickier since you have to subtract Saturday/Sunday. To calculate the number of WEEKdays between two date fields do this:

function workingDaysBetweenDates(@startDate, @endDate) {

// Validate input
if (@endDate < @startDate)
return 0;

// Calculate days between dates
var millisecondsPerDay = 86400 * 1000; // Day in milliseconds
@startDate.setHours(0,0,0,1); // Start just after midnight
@endDate.setHours(23,59,59,999); // End just before midnight
var diff = @endDate - startDate; // Milliseconds between datetime objects
var days = Math.ceil(diff / millisecondsPerDay);

// Subtract two weekend days for every week in between
var weeks = Math.floor(days / 7);
var days = days - (weeks * 2);
// Handle special cases
var startDay = @startDate.getDay();
var endDay = @endDate.getDay();

// Remove weekend not previously removed.
if (@startDay - @endDay > 1)
days = days - 2;

// Remove start day if span starts on Sunday but ends before Saturday
if (@startDay == 0 && @endDay != 6)
days = days - 1

// Remove end day if span ends on Saturday but starts after Sunday
if (@endDay == 6 && @startDay != 0)
days = days - 1

return days;
}
workingDaysBetweenDates( new Date(@StartDate) , new Date(@EndDate))

And there you have it! You have successfully calculated the number of weekdays. Now you have a variety of options for calculating time between two dates in Podio.

TECHeGO
Use Podio to calculate the time between two dates.
• No video selected.