subtract long number in javascript

Dear Experts,

I try to find out  the running time for "for loop" , I guess probably it will be in milliseconds.
so I try to convert the new Date() into milli-second  and subtracting it before and after the
loop code. but when I console.log(c-b); it will show wrong answer, why ?
How to define the b, c in double or long format for number  and get the correct result of "c-b"
I expect it should be c-b=80 in milli seconds

Pleas advise

Rwniceing

Wrong console.log output   from the following code
1- 1407193070584
2- 1407193070664
3- 1407166064013  //it is wrong I think
var i, a, b, c, max;
 max = 2e7;
 var d = Date.now();
 var a = new Date();
var y= a.getFullYear();
var m=a.getMonth();
var d=a.getDay();
var b = Date.UTC(y,m,d,a.getHours(),a.getMinutes(),a.getSeconds(),a.getMilliseconds());
console.log("1-",b);
for (i = 0; i < max; i++) {
    a = 1234 + 5678 + i;
    b = 1234 * 5678 + i;
    c = 1234 / 2 + i;
}
var a = new Date();
var y= a.getFullYear();
var m=a.getMonth();
var d=a.getDay();
var c = Date.UTC(y,m,d,a.getHours(),a.getMinutes(),a.getSeconds(),a.getMilliseconds());
console.log("2-",c); 
console.log("3-",c-b);

Open in new window

LVL 1
rwniceingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

COBOLdinosaurCommented:
avascript does not do math very well.

you might be better to use the raw date value in micro second with a.valueOf(); and use those values for the calculation. Reference here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/valueOf

Cd&
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rwniceingAuthor Commented:
Done with .valueOf() , thanks for your reply
0
Ray PaseurCommented:
This is kind of simplified, but it seemed to work OK.  Only tested for very short intervals, and it probably would need at least the addition of minutes to achieve greater reliability.  That said, using the confirm() and clicking it quickly I was able to get down to about 900ms.
http://iconoun.com/demo/temp_rwniceing.php

<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>

<!-- SEE http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28507319.html -->

<meta charset="utf-8" />
<meta name="robots" content="noindex, nofollow" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">


<title>HTML5 Page with Simple JavaScript Elapsed Time (ms) Calculation</title>
</head>
<body>

<script>
var ad = new Date();
var am = ad.getMilliseconds();
var as = ad.getSeconds();

var xx = confirm('Continue?');

var zd = new Date();
var zm = zd.getMilliseconds();
var zs = zd.getSeconds();

var az = (zs * 1000 + zm) - (as * 1000 + am);
alert('This took ' + az + ' ms.');
</script>

</body>
</html>

Open in new window

0
rwniceingAuthor Commented:
When I change those Date variable  from a to a2 ,b to b2 and c to c2 in previous post code. it will echo out the correct result on "c2-b2" without using .valueOf() function. I don;t know why ?
I have checked the previous code that is okay that there is no any  variable conflict since a, b,c
variable in the for loop  that doesn't affect the date() extraction.

Any expert could test those post code (previous on topic code and this post code as follows) to see what I say?

var i, a, b, c, max;
 max = 2e7;
 var d = Date.now();
 var a2 = new Date();
var y= a2.getFullYear();
var m=a2.getMonth();
var d=a2.getDay();
var b2 = Date.UTC(y,m,d,a2.getHours(),a2.getMinutes(),a2.getSeconds(),a2.getMilliseconds());
console.log("1-",b2);
for (i = 0; i < max; i++) {
    a = 1234 + 5678 + i;
    b = 1234 * 5678 + i;
    c = 1234 / 2 + i;
}
var a2 = new Date();
var y= a2.getFullYear();
var m=a2.getMonth();
var d=a2.getDay();
var c2 = Date.UTC(y,m,d,a2.getHours(),a2.getMinutes(),a2.getSeconds(),a2.getMilliseconds());
console.log("2-",c2); 
console.log("3-",c2-b2);

Open in new window

0
rwniceingAuthor Commented:
Sorry again, the topic post code is wrong since  variable b

at  var b = Date.UTC(y,m,d,a.getHours(),a.getMinutes(),a.getSeconds(),a.getMilliseconds());

is overwritten by b in for loop so that came out the wrong output

Conclude again  topic  post code with the mistake corrected by changed b to b2  for date that works , and  .valueOf() also works if variable assignment is correct

this post is solved and closed.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.