1

I just want to change the shape of the numbers from Persian to English(i mean like this 1400/06/31). I mean to keep it Persian. Only the text font should be English. How can I do this?

 var today = new Date().toLocaleDateString('fa-IR');
  var field = document.querySelector('#tday');
  field.value = today;
<input type="text" name="tday" class="form-control w-100 text-center d-inline-block" id="tday">
smmmm
  • 61
  • 5
  • Inside of a single input field, you can not have multiple different fonts to begin with. (Assuming you are asking about fonts? Not sure what exactly you mean by "shape" otherwise.) – CBroe Sep 22 '21 at 11:22
  • seems unclear please edit your question and add more description – Vishnu Bhadoriya Sep 22 '21 at 11:23
  • @CBroe i mean like this 1400/06/31 – smmmm Sep 22 '21 at 11:24
  • Theoretically it's possible. You'll need to customize a font, which displays Persian number characters as English number glyphs. – hackape Sep 22 '21 at 11:24
  • @hackape How can i do it?? – smmmm Sep 22 '21 at 11:26
  • CBroe is right, you cannot use multiple font within an input element. So this isn't a programming problem. Find a font designer who knows how to customize font. – hackape Sep 22 '21 at 11:26
  • Are the initial numbers English or Persian? 0 1 2 or ٠ ١ ٢? – Shahriar Sep 22 '21 at 11:26
  • @Shahriar in the main page they are English – smmmm Sep 22 '21 at 11:28
  • @smmmm do yo want to convert ۱۴۰۰/۰۶/۳۱ to 1400/06/31? – Shahriar Sep 22 '21 at 11:28
  • @Shahriar yesssss – smmmm Sep 22 '21 at 11:28
  • A quick search shows several questions which seem to answer your question, have you tried those? https://stackoverflow.com/questions/31439604/how-to-convert-persian-and-arabic-digits-of-a-string-to-english-using-javascript, https://stackoverflow.com/questions/11766726/convert-persian-arabic-numbers-to-english-numbers, https://stackoverflow.com/questions/35288093/jquery-input-number-convert-persian-arabic-numbers-to-english-numbers, https://stackoverflow.com/questions/43225416/convert-only-arabic-numbers-to-english-in-a-text-input ... – Don't Panic Sep 22 '21 at 11:42
  • Does this answer your question? [How to convert Persian and Arabic digits of a string to English using JavaScript?](https://stackoverflow.com/questions/31439604/how-to-convert-persian-and-arabic-digits-of-a-string-to-english-using-javascript) – Don't Panic Sep 22 '21 at 11:43

3 Answers3

3

You can use this function to convert Persian numbers to English numbers

export default function toEnglishDigits(num) {

  const id = {
    '۰': '0',
    '۱': '1',
    '۲': '2',
    '۳': '3',
    '۴': '4',
    '۵': '5',
    '۶': '6',
    '۷': '7',
    '۸': '8',
    '۹': '9',
  }
  return num ? num.toString().replace(/[^0-9.]/g, function (w) {
    return id[w] || w
  }) : null
}

and use this to convert English numbers to Persian numbers

export default function toPersianDigits (num) {
  if (num?.toString()) {
    const persianNumbers =
      '\u06F0\u06F1\u06F2\u06F3\u06F4\u06F5\u06F6\u06F7\u06F8\u06F9'
    return new String(num).replace(/[0123456789]/g, (d) => {
      return persianNumbers[d]
    })
  }
  return num
}

you can also pass strings to these functions with any format.

1

To convert English (Arabic) numbers to Persian (Farsi) numbers, the following one-liner code will do the job.

It will change all Farsi numbers but keep everything else unchanged including the position of all other text.

const toFa = n => n.replace(/\d/g, d => "۰۱۲۳۴۵۶۷۸۹"[d])

// Tests
console.log(toFa("1400/06/31"));             // "۱۴۰۰/۰۶/۳۱"
console.log(toFa("The number is 25,000"));   // "The number is ۲۵,۰۰۰"
console.log(toFa("عدد 1234.78 است"));         // "عدد ۱۲۳۴.۷۸ است"
console.log(toFa("من متولد 1975/10/25 هستم"));  //"من متولد ۲۳/۱۰/۱۹۷۵ هستم"
Dharman
  • 30,962
  • 25
  • 85
  • 135
Mohsen Alyafei
  • 4,765
  • 3
  • 30
  • 42
0

If you are okay with adding a package, use Persian Tools digitsFaToEn function:

var englishNumber = digitsFaToEn("۱۴۰۰/۰۶/۳۱")  // englishNumber = 1400/06/31

https://github.com/persian-tools/persian-tools

Shahriar
  • 1,855
  • 2
  • 21
  • 45