admin管理员组文章数量:1336367
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);
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);
Share
Improve this question
asked Jul 27, 2014 at 12:12
Prince SinghPrince Singh
5,1835 gold badges29 silver badges33 bronze badges
2
- what effort have you made? – Daniel A. White Commented Jul 27, 2014 at 12:14
- @pr1nc3 What do the other two output? – Sumurai8 Commented Jul 27, 2014 at 17:41
3 Answers
Reset to default 4Since you are working with elapsed time and not time of day, then I remend 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;
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/6q3WB/.
var baseDate = new Date();
timeElapsed = "01:23:00", // time elapsed
timeElapsedParts = timeElapsed.split(':'),
timeElapsedDate = new Date(),
timeElapsedInMilliseconds = 0,
timeTotal = "07:34:00", // total time
timeTotalParts = timeTotal.split(':'),
timeTotalDate = new Date(),
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 + '%');
本文标签: jqueryDifference Between two times as percentage in javascriptStack Overflow
版权声明:本文标题:jquery - Difference Between two times as percentage in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742355938a2459411.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论