Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

120
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda