Count-Up Javascript Code Question

Discussion in 'JavaScript' started by sport302, Nov 28, 2016.

  1. #1
    Can someone explain how I can get the following count-up javascript code to include a comma when a number grows past three digits. Right now for example the code will only display 1000 instead of the proper 1,000.

    Source for this code: http://www.javascriptkit.com/script/script2/countup.shtml

    Thanks...

    
    <script>
    var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
    
    function countup(yr,m,d){
    var today=new Date()
    var todayy=today.getYear()
    if (todayy < 1000)
    todayy+=1900
    var todaym=today.getMonth()
    var todayd=today.getDate()
    var todaystring=montharray[todaym]+" "+todayd+", "+todayy
    var paststring=montharray[m-1]+" "+d+", "+yr
    var difference=(Math.round((Date.parse(todaystring)-Date.parse(paststring))/(24*60*60*1000))*1)
    difference+=" days"
    document.write("It\'s been "+difference+" since the launch of JavaScript Kit!")
    }
    //enter the count up date using the format year/month/day
    countup(1997,12,05)
    </script>
    Code (JavaScript):
     
    Solved! View solution.
    sport302, Nov 28, 2016 IP
  2. #2
    I just looked up what function can do that and put it into your code:

    https://jsfiddle.net/13k83mu7/

    
    var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
    
    function countup(yr,m,d){
    var today=new Date()
    var todayy=today.getYear()
    if (todayy < 1000)
    todayy+=1900
    var todaym=today.getMonth()
    var todayd=today.getDate()
    var todaystring=montharray[todaym]+" "+todayd+", "+todayy
    var paststring=montharray[m-1]+" "+d+", "+yr
    var difference=(Math.round((Date.parse(todaystring)-Date.parse(paststring))/(24*60*60*1000))*1)
    difference+=" days"
    function formatNumber (difference) {
        return difference.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,")
    }
    document.write("It\'s been "+formatNumber(difference)+" since the launch of JavaScript Kit!")
    }
    //enter the count up date using the format year/month/day
    countup(1997,12,05)
    
    
    Code (markup):
    I am sure there are more efficient ways of doing the same thing.
     
    qwikad.com, Nov 28, 2016 IP
  3. sport302

    sport302 Well-Known Member

    Messages:
    351
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #3
    Thank you very much the above code appears to have fixed my issue.
     
    sport302, Nov 28, 2016 IP