Animate numbers from 0 to Value with Comma - Value is pulled from var

Anthony Whitefield

Struggling with this one so any ideas or suggestions are really appreciated.

I have a calculation (working) that counts how many days have passed since 01/01/2021 and * by a value. I would like to animate from 0 to the value over a period of 3 seconds, however, I don't know how to get around commas - can get it working without.

Any suggestions?

var startDate = new Date('01-01-2021');
var today = new Date();
var diff = Math.floor((today - startDate)/(1000*60*60*24))
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0');
var yyyy = today.getFullYear();
today = mm + '/' + dd + '/' + yyyy;
var answer = diff * 134256;
 
function commaSeparateNumber(val){
    while (/(\d+)(\d{3})/.test(val.toString())){
      val = val.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
    }
    return val;
}
document.getElementById("CCalc").innerHTML = (commaSeparateNumber(answer));
<p id="CCalc" class="fig-number"></p>

Adrian Biba

Here is the code with jQuery:

var startDate = new Date('01-01-2021');
var today = new Date();
var diff = Math.floor((today - startDate)/(1000*60*60*24))
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0');
var yyyy = today.getFullYear();
today = mm + '/' + dd + '/' + yyyy;
var answer = diff * 134256;
 
function commaSeparateNumber(val){
    while (/(\d+)(\d{3})/.test(val.toString())){
      val = val.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
    }
    return val;
}
document.getElementById("CCalc").innerHTML = parseFloat(commaSeparateNumber(answer).replace(/,/g, ''));


$(".fig-number").each(function () {
    $(this)
      .prop("Counter", 0)
      .animate(
        {
          Counter: $(this).text()
        },
        {
          duration: 3000,
          easing: "swing",
          step: function (now, tween) {
            // Check added for decimal number
            if(parseInt(tween.end) == parseFloat(tween.end)){
                var number = Math.ceil(now);
              $(this).text(number.toLocaleString());
            }
          },
        }
      );
  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<p id="CCalc" class="fig-number">39471264</p>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get rows from Database from comma-separated value

Animate CAShapeLayer path from old value to new value in Swift

match the number from comma separated value and count

Select a value from a comma seperated column

Remove duplicate comma seperated numbers from input text value in jQuery

A problem with grabbing var value from getElementById

Replacing a pulled SQL ID value with its name from another table

Remove comma from last array value

GPS value pulled from iOS image is not showing proper Positive or Negative

Find specific pattern from var value

Remove Last Comma From Array Value in Loop

Get a specific value from comma separated string

select a value from array based on var

Get value from script to var in mvc

Assign value to var in Smarty from specific values

update a column of numbers from value to (value * 1.3)

replace dots and comma from a QRegExp unicode value

Remove comma from value box in array

P value of Portmanteau Test from VAR model

How to set var value from a datasource file?

How to get var value from a text file?

mysql join get column value from comma separated value

Comma separated value from array

How to make a regex expression that will extract all cell numbers from a comma separated list of cells and subtract a constant value from each one?

Firebase Save Data from Comma Separated Value

How to convert string value returned from oracle apex 20.1 multiselect item into comma separated numbers array

Ansible List acces from value var

How to convert comma separated numbers from a dataframe to to numbers and get the avg value

create a comma separated string from space separated var value in bash