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 out the percentage of it ?
Update
Tried this but in console I see "NaN"
var TimeLeft = Date.parse(TimeLft.text())/100;
console.log(TimeLeft);
var Totaltime = Date.parse(DealTotalTm)/100;
console.log(Totaltime);
var percentChange = (TimeLeft/Totaltime)*100;
console.log(percentChange);
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:
function totalSeconds(time){
var parts = time.split(':');
return parts[0] * 3600 + parts[1] * 60 + parts[2];
}
var timeone = "01:23:00"; // time elapsed
var 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.
"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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With