I want to get the difference between two times. The first one is the current time object that I get using javascript date object new Date()
And the second one is a string like "20:30".
So suppose if current time is 18:00 then I just want the difference like that "2 hours and 30 mins left.
Thanks for any type of help.
Asked
Active
Viewed 126 times
-1

Sagar Jajoriya
- 2,377
- 1
- 9
- 17
-
I think you could read the question here. [date-diff-javascript](http://stackoverflow.com/questions/34386175/date-diff-javascript) – dawnfly May 20 '17 at 13:33
-
2Possible duplicate of [Check time difference in Javascript](http://stackoverflow.com/questions/1787939/check-time-difference-in-javascript) – mehulmpt May 20 '17 at 13:37
6 Answers
1
$(document).ready(function() {
function calculateTime() {
var valuestop = $("select[name='timestop']").val();
var timeStart = new Date().getTime();//current time
var timeEnd = new Date(new Date().toJSON().slice(0, 10).replace(/-/g, '/') + ' ' + valuestop).getTime();//selected time
console.log(timeStart + ' ' + timeEnd);
var hourDiff = timeEnd - timeStart; //in ms
var secDiff = hourDiff / 1000; //in s
var minDiff = hourDiff / 60 / 1000; //in minutes
var hDiff = hourDiff / 3600 / 1000; //in hours
var humanReadable = {};
humanReadable.hours = Math.floor(hDiff);
humanReadable.minutes = Math.round(minDiff - 60 * humanReadable.hours);
console.log(humanReadable); //{hours: 0, minutes: 30}
$("p").html("Difference: <b>Hours: </b>" + humanReadable.hours + "<b>Minutes: </b>" + humanReadable.minutes);
}
$("select").change(calculateTime);
calculateTime();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="timestop">
<option value="00:00:00">12:00 am</option>
<option value="01:00:00">1:00 am</option>
<option value="02:00:00">2:00 am</option>
<option value="03:00:00">3:00 am</option>
<option value="04:00:00">4:00 am</option>
<option value="05:00:00">5:00 am</option>
<option value="06:00:00">6:00 am</option>
<option value="07:00:00">7:00 am</option>
<option value="08:00:00">8:00 am</option>
<option value="09:00:00">9:00 am</option>
<option value="10:00:00">10:00 am</option>
<option value="11:00:00">11:00 am</option>
<option value="12:00:00">12:00 pm</option>
<option value="13:00:00">1:00 pm</option>
<option value="14:00:00">2:00 pm</option>
<option value="15:00:00">3:00 pm</option>
<option value="16:00:00">4:00 pm</option>
<option value="17:00:00">5:00 pm</option>
<option value="18:00:00">6:00 pm</option>
<option value="19:00:00">7:00 pm</option>
<option value="20:00:00">8:00 pm</option>
<option value="21:00:00">9:00 pm</option>
<option value="22:00:00">10:00 pm</option>
<option value="23:00:00">11:00 pm</option>
</select>
<p> </p>

Keshan Nageswaran
- 8,060
- 3
- 28
- 45
0
Easiest is to use the library moment.js which can handle time/date-manipulation well.

rymdmaskin
- 508
- 5
- 13
-
Is there any way to do that using available javascript Date functions ? – Sagar Jajoriya May 20 '17 at 13:30
-
@SKJajoriya yes, you can parse the date with `Date` if you want, see this thread for example: http://stackoverflow.com/questions/5619202/converting-string-to-date-in-js And the just compare the two dates and get the difference. – rymdmaskin May 20 '17 at 13:33
0
Try like below:
var date1 = new Date(2000, 0, 1, 20, 30);
var date2 = new Date(2000, 0, 1, 18, 0);
var diff = date1-date2;//milli seconds
var min = diff / 1000 / 60;
var r = min % 1;
var h=min/60;
var sec = Math.floor(r * 60);
if (sec < 10) {
sec = '0'+sec;
}
alert(min+'-'+ sec);

lalithkumar
- 3,480
- 4
- 24
- 40
0
Utilize functions that are available in Date
object.
var origTime = '20:30'.split(':');
var dateA = new Date();
dateA.setHours(origTime[0]);
dateA.setMinutes(origTime[1]);
var dateB = new Date();
var hourDiff = Math.abs(dateA - dateB)/36e5;

P Pang
- 254
- 3
- 15
0
Here is how you can do it using only vanilla Js:
var compareDate = new Date();
var timeParts = "20:30".split(":");
compareDate.setHours(Number(timeParts[0]));
compareDate.setMinutes(Number(timeParts[0]));
compareDate.setSeconds(0);
compareDate.setMilliseconds(0);
var diff = compareDate - new Date();
var diffInMinutes = Math.floor(diff / 1000 / 60);
var hours = Math.floor(diffInMinutes / 60);
var minutes = diffInMinutes % 60;
hours + " hours and " + minutes + " minutes left";
We're "normalizing" seconds and milliseconds by setting them to 0, otherwise they'll be whatever they were when new Date()
was called.

Lennholm
- 7,205
- 1
- 21
- 30