javascriptjquerydatecounterfarsi

Convert Numbers of date to Persian numbers


I want numbers of date to be converted into the Persian numbers , I tried, it converts all of numbers in page except date

Here is my code Demo https://jsfiddle.net/dpcu2o57/16/

/* Counter */
var countDownDate = new Date("Dec 5, 2022 15:37:25").getTime();

var x = setInterval(function() {

  var now = new Date().getTime();

  var distance = countDownDate - now;

  var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);

  document.getElementById("demo").innerHTML = days + "d " + hours + "h " +
    minutes + "m " + seconds + "s ";

  if (distance < 0) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = "EXPIRED";
  }
}, 1000);



/*English Number to Persian */
function walkNode(node) {
  if (node.nodeType == 3) {
    node.data = node.data.replace(/\d/g, convert)
  }

  for (var i = 0; i < node.childNodes.length; i++) {
    walkNode(node.childNodes[i])
  }
}

walkNode(document.getElementsByTagName('body')[0])

function convert(a) {
  return ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'][a]
}
<p id="demo"></p>
<h4>1234567</h4>


Solution

  • You're using an interval timer to update the text with the default digits, but then not calling walkNode to convert them to Persian.

    You could add a call to walkNode to do that, but I wouldn't; instead, I'd convert the string before adding it to the DOM:

    document.getElementById("demo").innerHTML = convertString(days + "d " + hours + "h " +
        minutes + "m " + seconds + "s ");
    

    ...where convertString is:

    function convertString(str) {
        return str.replace(/\d/g, convert);
    }
    

    ...and I'd update walkNode to use convertString rather than an inline replace.

    Live Example:

    /* Counter */
    var countDownDate = new Date("Dec 5, 2022 15:37:25").getTime();
    
    var x = setInterval(function() {
    
      var now = new Date().getTime();
    
      var distance = countDownDate - now;
    
      var days = Math.floor(distance / (1000 * 60 * 60 * 24));
      var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
      var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
      var seconds = Math.floor((distance % (1000 * 60)) / 1000);
    
      document.getElementById("demo").innerHTML = convertString(days + "d " + hours + "h " +
        minutes + "m " + seconds + "s ");
    
      if (distance < 0) {
        clearInterval(x);
        document.getElementById("demo").innerHTML = "EXPIRED";
      }
    }, 1000);
    
    
    
    /*English Number to Persian */
    function walkNode(node) {
      if (node.nodeType == 3) {
        node.data = convertString(node.data);
      }
    
      for (var i = 0; i < node.childNodes.length; i++) {
        walkNode(node.childNodes[i])
      }
    }
    
    walkNode(document.getElementsByTagName('body')[0])
    
    function convert(a) {
      return ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'][a]
    }
    function convertString(str) {
        return str.replace(/\d/g, convert);
    }
    <p id="demo"></p>
    <h4>1234567</h4>