Calculate time difference from two Date fields

markdown17.10

 

Let’s say you have two Date fields, and you want to show their time difference in hours and minutes.

We will create a Hours Calculation field, and a Minutes Calculation field. Just copy paste the following code for Hours:

 

moment(@Departure Time).diff(moment(@Arrival Time), ‘hours’)

/* ——————————————————————————————————-

Created by BendixKiel ApS – Copenhagen based Podio Preferred Partner -with more than 6 years of experience in helping organizations implement Podio. Please feel free to contact us if you need help evaluate, structure or implement Podio in your organization.

BendixKiel ApS www.bendixkiel.org +45 71 99 05 80 anders@bendixkiel.org

———————————————————————————————————– */

 

 

Also copy paste the following code for Minutes:

 var h = @Hours;

var tm = moment(@Departure Time).diff(moment(@Arrival Time), ‘minutes’);

(h == 0) ? tm :
           tm % (h * 60) 

/* ——————————————————————————————————-

Created by BendixKiel ApS – Copenhagen based Podio Preferred Partner -with more than 6 years of experience in helping organizations implement Podio. Please feel free to contact us if you need help evaluate, structure or implement Podio in your organization.

BendixKiel ApS www.bendixkiel.org +45 71 99 05 80 anders@bendixkiel.org

———————————————————————————————————– */

 

Don’t forget to replace @Arrival Time, @Departure Time with your two Date fields that you want to get the difference. Also replace @Hours with the name that you have given to your new Hours field.

Both Hours and Minutes Calculation fields become Number fields.

 

Thus, the following changes in “Modify Template”

markdown17.1

 

give us the following result

markdown17.2

 

The calculation also works fine for different dates.

markdown17.3

 

 

If you want to show the time difference in just one Calculation field, just copy paste the following code:

var d = @Departure Time;
var a = @Arrival Time;

var h = moment(d).diff(moment(a), ‘hours’);
var tm = moment(d).diff(moment(a), ‘minutes’);
var m = (h == 0) ? tm :
                   tm % (h * 60);

h.toString() + ” hours and ” + m.toString()+ ” minutes” 

/* ——————————————————————————————————-

Created by BendixKiel ApS – Copenhagen based Podio Preferred Partner -with more than 6 years of experience in helping organizations implement Podio. Please feel free to contact us if you need help evaluate, structure or implement Podio in your organization.

BendixKiel ApS www.bendixkiel.org +45 71 99 05 80 anders@bendixkiel.org

———————————————————————————————————– */

 

Don’t forget to replace @Arrival Time, @Departure Time with your two Date fields that you want to get the difference. Calculation field has now become a Text field.

 

Thus, the following changes in “Modify Template”

markdown17.4

 

give us the following result:

markdown17.5

 

There is also an updated version: In case there are no hours, the Calculation field does not show any hours. Same thing happens with minutes. Moreover, if you have 1 hour or 1 minute, then the Calculation field does not show 1 hours or 1 minutes. Just
 the following code:

 

var d = @Departure Time;
var a = @Arrival Time;

var h = moment(d).diff(moment(a), ‘hours’);
var tm = moment(d).diff(moment(a), ‘minutes’);
var m = (h == 0) ? tm :
                   tm % (h * 60);

var dif = (h == 0) ? “” : 
          (h == 1) ? h.toString()+ ” hour” :
                     h.toString()+ ” hours”;

dif += (h == 0 || m == 0) ? “” : ” and “;

dif += (h == 0 && m == 0) ? “No time difference” :
       (m == 0) ? “” : 
       (m == 1) ? m.toString() + ” minute”  :
                  m.toString() + ” minutes”;
dif 

/* ——————————————————————————————————-

Created by BendixKiel ApS – Copenhagen based Podio Preferred Partner -with more than 6 years of experience in helping organizations implement Podio. Please feel free to contact us if you need help evaluate, structure or implement Podio in your organization.

BendixKiel ApS www.bendixkiel.org +45 71 99 05 80 anders@bendixkiel.org

———————————————————————————————————– */

 

Don’t forget to replace @Arrival Time, @Departure Time with your two Date fields that you want to get the difference.

Thus, the following changes in “Modify Template”

markdown17.6

 

give us the following results:

 

markdown17.9 markdown17.8 markdown17.7 markdown17.10

 

 

No comments yet.

Leave a Reply