将日期数字转换为波斯数字

Convert Numbers of date to Persian numbers

我想将日期数字转换成波斯数字,我试过了,它转换了页面中除日期之外的所有数字

这是我的代码Demohttps://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>

您正在使用间隔计时器使用默认数字更新文本,但没有调用 walkNode 将它们转换为波斯语。

您可以添加对 walkNode 的调用来执行此操作,但我不会;相反,我会先转换字符串,然后再将其添加到 DOM:

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

...其中 convertString 是:

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

...我会更新 walkNode 以使用 convertString 而不是内联替换。

实例:

/* 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>