Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

123
Visualizações
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>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda