Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

subtract long number in javascript

Posted on 2014-08-28
6
Medium Priority
?
193 Views
Last Modified: 2014-08-28
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

0
Comment
Question by:rwniceing
  • 3
5 Comments
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 2000 total points
ID: 40290588
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
 
LVL 1

Author Closing Comment

by:rwniceing
ID: 40290595
Done with .valueOf() , thanks for your reply
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40290740
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
 
LVL 1

Author Comment

by:rwniceing
ID: 40290770
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
 
LVL 1

Author Comment

by:rwniceing
ID: 40290820
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question