Can anyone help me to change Gregorian date to Persian in JavaScript? I want to use it in HTML and JavaScript.
To enable the Persian calendar, go into Settings, click on the General tab, and select it as an Alternate calendar.
In Iran, short dates are written as year/month/day, for example ۱۳۸۹/۵/۱۶, and long dates as day month name year from right to left, for example ۱۶ مرداد ۱۳۸۹. Both two-digit and four-digit years are valid but months and days are not usually padded with leading zeros.
Today is Tuesday 18 Muharram (1) 1444 AH corresponding to 16 August - Ab (8) 2022 AD.
DD/MM/YYYY - European style Gregorian date format. YYYY/MM/DD - Sortable style Gregorian date format.
You can use toLocaleDateString();
let today = new Date().toLocaleDateString('fa-IR'); console.log(today);
fa-IR
is for Farsi-Iran,
other dates is as follow:
en-US
: For English
hi-IN
: For Hindi
...
also you can set options as second argument, for example:
let options = { year: 'numeric', month: 'long', day: 'numeric' }; new Date().toLocaleDateString('fa-IR', options);
and in order to convert characters to Latin digits you can do:
let today = new Date().toLocaleDateString('fa-IR'); today.replace(/([۰-۹])/g, token => String.fromCharCode(token.charCodeAt(0) - 1728));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With