Skip to content Skip to sidebar Skip to footer

Difference Between Two Times As Percentage In Javascript

example say : var timeone = '01:23:00'; // time elapsed var timetwo = '07:34:00'; // total time then how can i calculate the difference between total & elapsed time and take

Solution 1:

Since you are working with elapsed time and not time of day, then I recommend avoiding the Date object. It's intended for a different purpose.

If you can guarantee the inputs are in the hour:minute:second format that you specified, then simply split the parts to calculate the total seconds as a single integer. You can then divide them to obtain the percentage, like this:

functiontotalSeconds(time){
    var parts = time.split(':');
    return parts[0] * 3600 + parts[1] * 60 + parts[2];
}

var timeone = "01:23:00"; // time elapsedvar timetwo = "07:34:00"; // total time var pct = (100 * totalSeconds(timeone) / totalSeconds(timetwo)).toFixed(2);

I've fixed the results to two decimal places, but you can adjust if you like.

jsFiddle here.

Solution 2:

"01:23:00" contains only a time part and is not a valid format that can be parsed using Date.parse()

To get it to parse correctly, prefix a dummy date part before parsing.

For example: Date.parse("01-01-2000 " + TimeLft.text())

Since you are looking to get a percentage value, you need to then subtract the seconds added for the date part (01-01-2000) from the result:

i.e.

var dayOffset = Date.parse("01-01-2000 00:00:00");
var left =  Date.parse("01-01-2000 " + TimeLft.text()) - dayOffset;
var total =  Date.parse("01-01-2000 " + DealTotalTm) - dayOffset;
var percentChange = (TimeLeft/Totaltime)*100;

Solution 3:

First of Date.parse() isn't properly supported in all browsers (IE lt 9), so I would suggest you use the Date objects .set*() functions. Second, since your times doesn't have any date, you need to base them against start of the day. This should work and be browser safe http://jsfiddle.net/6q3WB/.

var baseDate = newDate();
    timeElapsed = "01:23:00", // time elapsed
    timeElapsedParts = timeElapsed.split(':'),
    timeElapsedDate = newDate(),
    timeElapsedInMilliseconds = 0,
    timeTotal = "07:34:00", // total time
    timeTotalParts = timeTotal.split(':'),
    timeTotalDate = newDate(),
    timeTotalInMilliseconds = 0,
    timeDifferenceInPercentage = 0;

baseDate.setHours(0);
baseDate.setMinutes(0);
baseDate.setSeconds(0);
baseDate.setMilliseconds(0);

timeElapsedDate.setHours(timeElapsedParts[0]);
timeElapsedDate.setMinutes(timeElapsedParts[1]);
timeElapsedDate.setSeconds(timeElapsedParts[2]);
timeElapsedDate.setMilliseconds(0);

timeTotalDate.setHours(timeTotalParts[0]);
timeTotalDate.setMinutes(timeTotalParts[1]);
timeTotalDate.setSeconds(timeTotalParts[2]);
timeTotalDate.setMilliseconds(0);

timeElapsedInMilliseconds = (timeElapsedDate.getTime() - baseDate.getTime());
timeTotalInMilliseconds = (timeTotalDate.getTime() - baseDate.getTime());

timeDifferenceInPercentage = (Math.round(((timeTotalInMilliseconds - timeElapsedInMilliseconds) / timeTotalInMilliseconds) * 10000) / 100);

console.log(timeDifferenceInPercentage + '%');

Post a Comment for "Difference Between Two Times As Percentage In Javascript"