1

I have this Javascript

<script>
var START_DATE = new Date("February 21, 2018 00:00:00"); // put in the 
starting date here
var INTERVAL = 1; // in seconds
var INCREMENT = 6720; // increase per tick
var START_VALUE = 17419171608; // initial value when it's the start date
var count = 0;

window.onload = function()
{
 var msInterval = INTERVAL * 1000;
 var now = new Date();
 count = parseInt((now - START_DATE)/msInterval) * INCREMENT + START_VALUE;
 document.getElementById('counter').innerHTML = count;
 setInterval("count += INCREMENT; 
document.getElementById('counter').innerHTML = count;", msInterval);
}
</script>

I am trying to put commas to separate the thousands in the above code but I have been unable to do so.

I tried the instructions here, but I have not been able to put the commas separating the thousands.

Any help is highly appreciated. Thanks.

Hamza Ahmad
  • 512
  • 2
  • 7
  • 32
  • https://stackoverflow.com/questions/8677805/formatting-numbers-decimal-places-thousands-separators-etc-with-css#answer-28708770 – Mitya Mar 13 '18 at 12:31

3 Answers3

1

You can try using the Number.prototype.toLocaleString() function on your "count" variable. The number will be printed depending on the locale, so for EN it will have the expected commas.

var count = 10000000;    
console.log (count.toLocaleString());
harymitchell
  • 155
  • 7
1

Check this out: How to print a number with commas as thousands separators in JavaScript

There is a great function for parsing negative,large and floats.

You can try outputting your count after running it through this:

const numberWithCommas = (x) => {
  var parts = x.toString().split(".");
  parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  return parts.join(".");
}

Here is a quick fiddle i through together to show you the method: https://jsfiddle.net/r4s5rrrn/2/

DLzer
  • 159
  • 11
1

I take your example and i modifed.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
        <script type="text/javascript">
 
            var START_DATE = new Date("December 1, 2010 20:00:00"); // put in the starting date here
            var INTERVAL = 1; // in seconds
            var INCREMENT = 1; // increase per tick
            var START_VALUE = 4166667; // initial value when it's the start date
            var count = 0;
 
            window.onload = function() {
                var msInterval = INTERVAL * 1000;
                var now = new Date();
                count = parseInt((now - START_DATE)/msInterval) * INCREMENT + START_VALUE;
                document.getElementById('counter').innerHTML = addCommas(count.toString());
                setInterval(incrCount, msInterval);
            }
 
            function incrCount() {
                count += INCREMENT;
                document.getElementById('counter').innerHTML = addCommas(count.toString());
            }
 
           
   
   function addCommas(nStr){
                return parseInt(nStr).toLocaleString();
            }
        </script>
    </head>
    <body>
 
        <div id="counter"></div>
 
    </body>
</html>
 
Jesus Cuesta
  • 175
  • 4