0

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>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Amin
  • 31
  • 5
  • You need the `walkNode` inside the interval function: `walkNode(document.getElementsByTagName('body')[0]) }, 1000);` – mplungjan Jan 13 '22 at 10:19
  • 1
    Separately, you can use [`toLocaleString`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString) on the `Date` object to have it output a string appropriate to the current locale (or one you specify). – T.J. Crowder Jan 13 '22 at 10:21
  • It seems unusual to use Persian numbers with English letters. If you want the date in the Persian language, you can use `new Date().toLocaleString('fa')`, however with *toLocaleString* the format is principally determined from the language code, so if you want to format it differently you can use [*formatToParts*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/formatToParts) to get the parts and arrange them as required. – RobG Jan 14 '22 at 02:41

1 Answers1

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>
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875