How To Get Of Two Time Values Using Jquery/javascript?
Solution 1:
Writing your own time and date functions can get complex. Why re-invent the wheel. Take a look at the excellent http://www.datejs.com/ date library. It handles all date and time related tasks and usage is very simple.
Solution 2:
Here's something I had laying around. It allows for an infinite number of arguments, so you could have addTime('01:00')
or addTime('01:00', '02:00', '03:00', '04:00')
, etc. It's three functions long because it also verifies if the times entered are properly formatted, and if not, then it formats them. (E.g. Ensures that minutes is 2 digits long, and if hours is 1 digit long, then pad it with one zero, etc.)
You can play with it here: http://jsfiddle.net/WyxwU/
It's also here:
var totalTime = addTime('12:34', '56:12', '78:45');
document.write(totalTime);
functionaddTime()
{
if (arguments.length < 2)
{
if (arguments.length == 1 && isFormattedDate(arguments[0])) returnarguments[0];
elsereturnfalse;
}
var time1Split, time2Split, totalHours, totalMinutes;
if (isFormattedDate(arguments[0])) var totalTime = arguments[0];
elsereturnfalse;
for (var i = 1; i < arguments.length; i++)
{
// Add them up
time1Split = totalTime.split(':');
time2Split = arguments[i].split(':');
totalHours = parseInt(time1Split[0]) + parseInt(time2Split[0]);
totalMinutes = parseInt(time1Split[1]) + parseInt(time2Split[1]);
// If total minutes is more than 59, then convert to hours and minutesif (totalMinutes > 59)
{
totalHours += Math.floor(totalMinutes / 60);
totalMinutes = totalMinutes % 60;
}
totalTime = totalHours + ':' + padWithZeros(totalMinutes);
}
return totalTime;
}
functionisFormattedDate(date)
{
var splitDate = date.split(':');
if (splitDate.length == 2 && (parseInt(splitDate[0]) + '').length <= 2 && (parseInt(splitDate[1]) + '').length <= 2) returntrue;
elsereturnfalse;
}
functionpadWithZeros(number)
{
var lengthOfNumber = (parseInt(number) + '').length;
if (lengthOfNumber == 2) returnnumber;
elseif (lengthOfNumber == 1) return'0' + number;
elseif (lengthOfNumber == 0) return'00';
elsereturnfalse;
}
Solution 3:
Here is the simple JS code for this,
var a = "2:50";
var b = "2:15";
var splitTimeStr = function(t){
var t = t.split(":");
t[0] = Number(t[0]);
t[1] = Number(t[1]);
return t;
};
var addTime = function(t1, t2){
var t1Hr = splitTimeStr(t1)[0];
var t1Min = splitTimeStr(t1)[1];
var t2Hr = splitTimeStr(t2)[0];
var t2Min = splitTimeStr(t2)[1];
var rHr = t1Hr + t2Hr;
var rMin = t1Min + t2Min;
if (rMin >= 60)
{
rMin = rMin - 60;
rHr = rHr + 1;
}
if (rMin < 10) rMin = "0" + rMin;
if (rHr < 10) rHr = "0" + rHr;
return"" + rHr + ":" + rMin;
};
document.write(addTime(a, b));
you can validate/play this with code here: http://jsfiddle.net/z24v7/
Solution 4:
What you have to do is calculate them to a decimal by that I mean. Strip out the hour/mins multiple that by 60 + to mins
//strip out the hours l_hour = Number(l_time$.substr(0, l_pos)); //Strip out the mins l_min = Number(l_time$.substr(l_pos + 1, l_time$.length)); //add the two values divided by 60 mins l_time_decimal= Number(Math.abs(l_hour)) + Number(Math.abs(l_min)/60);
Do this for each value then deduct the two figures to give you the difference (i.e time taken). All thats left is convert it back from a decimal to a time l_difference_in_min = l_difference * 60; l_time_mins = l_difference_in_min%60; l_time_hours = (l_difference_in_min - l_mins)/60;
Now just format the two to be HH:MM
Solution 5:
I would break the problem into sub-tasks that are reusable. You have to concerns here:
- Process a time string in "hh:mm" format: Converting this to minutes makes sense because it seems to be the time granularity at which you're operating.
- Format a given number of minutes into a time string in "hh:mm" format.
The fact that you're adding two times together is a diversion from the actual two problems above.
Parse a time string into minutes:
functionparseMinutes(s) { var tokens = s.split(":"); return tokens[0] * 60 + parseInt(tokens[1]); }
Format minutes into a time string:
functionformatMinutes(minutes) { functionpad(n) { return n > 9 ? n : ("0" + n); } var hours = Math.floor(minutes / 60), mins = minutes % 60; returnpad(hours) + ":" + pad(mins); }
Then your specific problem can be tackled by:
var sum = formatMinutes(parseMinutes(a) + parseMinutes(b));
Post a Comment for "How To Get Of Two Time Values Using Jquery/javascript?"