Why toLocaleDateString with ar locale returns the date string in ddyyyy/m/ format?
Is ddyyyy/m/ legit date format in standard Arabic culture? Never seen a format like that.
new Date().toLocaleDateString('ar');
=> "15/1/2021"
Why toLocaleDateString with ar locale returns the date string in ddyyyy/m/ format?
Is ddyyyy/m/ legit date format in standard Arabic culture? Never seen a format like that.
new Date().toLocaleDateString('ar');
=> "15/1/2021"
Stored the formated date string in a variable and consoled chars, it is not ddyyyy/mm/, instead there are some special characters in-between. So the actual format is dd/mm/yyyy + right to left chars.
const str = new Date().toLocaleDateString('ar');
for(let i = 0; i < str.length; i++) {console.log(str[i])}
//Output
"1"
"5"
""
"/"
"1"
""
"/"
"2"
"0"
"2"
"1"
JSFiddle: https://jsfiddle.net/tsfahmad/wjq54o1a/
And then I also checked the charCode
str.charCodeAt(i);
49
53
8207 //<- It is right-to-left mark
47
49
8207 // <- Same (https://www.codetable.net/decimal/8207)
47
50
48
50
49
The returned date is with right-to-left code. See below when inserted into a div element, it will show correctly.
let today = new Date().toLocaleDateString('ar');
document.getElementById('myDiv').innerHTML = today;
<div id="myDiv" dir="rtl"> </div>