In JavaScript, how can I calculate the difference between two times that are in 24 hour format?
Example: Get how many hours elapsed from 08:00:00
to
It is not Jquery but everything related to time and date in JavaScript...
This might help: datejs http://code.google.com/p/datejs/
Here is some date and time calculation from the docs
Date.today().set({ day: 15 }) // Sets the day to the 15th of the current month and year. Other object values include year|month|day|hour|minute|second.
Date.today().set({ year: 2007, month: 1, day: 20 })
Date.today().add({ days: 2 }) // Adds 2 days to the Date. Other object values include year|month|day|hour|minute|second.
Date.today().add({ years: -1, months: 6, hours: 3 })
Date.today().addYears(1) // Add 1 year.
Date.today().addMonths(-2) // Subtract 2 months.
Date.today().addWeeks(1) // Add 1 week
Date.today().addHours(6) // Add 6 hours.
Date.today().addMinutes(-30) // Subtract 30 minutes
Date.today().addSeconds(15) // Add 15 seconds.
Date.today().addMilliseconds(200) // Add 200 milliseconds.
Date.today().moveToFirstDayOfMonth() // Returns the first day of the current month.
Date.today().moveToLastDayOfMonth() // Returns the last day of the current month.
new Date().clearTime() // Sets the time to 00:00 (start of the day).
Date.today().setTimeToNow() // Resets the time to the current time (now). The functional opposite of .clearTime()
EDIT:Since this is a rather old answer I lately do also use moment.js for date related operations really useful, too. http://momentjs.com/ https://github.com/moment/moment/
I changed your code to just use the difference without having to create another date object:
$(document).ready(function() {
function calculateTime() {
//get values
var valuestart = $("select[name='timestart']").val();
var valuestop = $("select[name='timestop']").val();
//create date format
var timeStart = new Date("01/01/2007 " + valuestart);
var timeEnd = new Date("01/01/2007 " + valuestop);
var difference = timeEnd - timeStart;
difference = difference / 60 / 60 / 1000;
$("p").html("<b>Hour Difference:</b> " + difference)
}
$("select").change(calculateTime);
calculateTime();
});
You dont need to convert them into dates for this specific case. Just Do this
$(document).ready(function() {
function calculateTime() {
var hourDiff =
parseInt($("select[name='timestart']").val().split(':')[0],10) -
parseInt($("select[name='timestop']").val().split(':')[0],10);
$("p").html("<b>Hour Difference:</b> " + hourDiff )
}
$("select").change(calculateTime);
calculateTime();
});
Working fiddle
You can just subtract the hours right away doing it this way
var valuestart = $("select[name='timestart']").val();
var valuestop = $("select[name='timestop']").val();
//create date format
var timeStart = new Date("01/01/2007 " + valuestart).getHours();
var timeEnd = new Date("01/01/2007 " + valuestop).getHours();
var hourDiff = timeEnd - timeStart;
Here's the working fiddle http://jsfiddle.net/VnwF7/4/
UPDATE - to calculate if we are including the next day. Just add the following if block
if (hourDiff < 0) {
hourDiff = 24 + hourDiff;
}
http://jsfiddle.net/gfvhqat9/
try this way
var time_start = new Date();
var time_end = new Date();
var value_start = "06:00:00".split(':');
var value_end = "23:00:00".split(':');
time_start.setHours(value_start[0], value_start[1], value_start[2], 0)
time_end.setHours(value_end[0], value_end[1], value_end[2], 0)
time_end - time_start // millisecond