Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Commas to JavaScript output

Tags:

javascript

I'm using the following script to count upward at an interval and it works perfectly. However, I'd like it to format the number with commas (56,181,995 instead of 56181995).

var START_DATE = new Date("July 27, 2010 13:30:00"); // put in the starting date here
var INTERVAL = 1; // in seconds
var INCREMENT = 2; // increase per tick
var START_VALUE = 101; // 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);
}

I thought I had found an answer here on SO but I can't get it to work:

How to print a number with commas as thousands separators in JavaScript

like image 462
brianrhea Avatar asked Nov 28 '22 03:11

brianrhea


1 Answers

(1234567890).toLocaleString();
like image 105
James Avatar answered Dec 09 '22 19:12

James