4

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);
Prince Singh
  • 5,023
  • 5
  • 28
  • 32

3 Answers3

4

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.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
1

"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;
techfoobar
  • 65,616
  • 14
  • 114
  • 135
1

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 = 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 + '%');
Niklas
  • 1,729
  • 1
  • 12
  • 19
  • [`Date.parse()` is supported in all major browsers.](http://www.w3schools.com/jsref/jsref_parse.asp) – Tiago Marinho Jul 27 '14 at 12:41
  • @TiagoMarinho Not i IE lt 9. I know this for a fact of own experience. Also, lots of treads supporting the fact: [http://stackoverflow.com/questions/11020658/javascript-json-date-parse-in-ie7-ie8-returns-nan](http://stackoverflow.com/questions/11020658/javascript-json-date-parse-in-ie7-ie8-returns-nan), [http://stackoverflow.com/questions/12513441/date-parse-doesnt-work-in-ie-8](http://stackoverflow.com/questions/12513441/date-parse-doesnt-work-in-ie-8) to list a few. – Niklas Jul 27 '14 at 12:55