4

I am using Jade Template Engine and have two bootstrap TimePickers. I want to calculate the difference between timings and show the difference in minutes.

TimePickers used:-

input#monthStartTimepicker.form control.timepicker(name='startTime',type="text", data-template="dropdown", data-default-time="00:00", data-show-meridian="false", data-minute-step="5", data-second-step="5" ,required)

input#monthEndTimepicker.form-control.timepicker(name='endTime',type="text", data-template="dropdown", data-default-time="00:00", data-show-meridian="false", data-minute-step="5", data-second-step="5" ,required)

Any ideas on how to achieve this?

Rohan Kandwal
  • 9,112
  • 8
  • 74
  • 107

2 Answers2

0

You can use moment to do them, this library has functions to effective work with dates. The inputs value has a dates, you can get the values using javascript and use moment to get the diff or send it with the form and use moment in the backend, if you are using nodejs is possible.

document.getElementById('monthStartTimepicker').value // this is the date value

Check this question: Get the time difference between two datetimes

Example:

var now  = "04/09/2013 15:00:00";
var then = "04/09/2013 14:20:30";

moment.utc(moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"))).format("HH:mm:ss")

// outputs: "00:39:30"
Rubén Soler
  • 1,147
  • 8
  • 23
0

If you want to use pure JS, you can create two date instances from the timepicker and simply subtract the time in milliseconds:

var date_1 = new Date("11/11/2000 " + valueFromPicker1); //HH:MM:SS format
var date_2 = new Date("11/11/2000 " + valueFromPicker2); //HH:MM:SS format

You could choose any date, as long its the same day in both date_1 and date_2.

Then calculate the difference:

var diff;
if (date_1.getTime() > date_2.getTime()) {
    diff = date_1.getTime() - date_2.getTime();
} else {
    diff = date_2.getTime() - date_1.getTime();
}

diff is the difference between these two times in milliseconds. After this you can simply convert diff to minutes or hours or whatever you need.

mind
  • 432
  • 6
  • 18