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">
CodePudding user response:
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.
CodePudding user response:
If you are okay with adding a package, use Persian Tools digitsFaToEn
function:
var englishNumber = digitsFaToEn("۱۴۰۰/۰۶/۳۱") // englishNumber = 1400/06/31