Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change Gregorian date to Persian date in JavaScript?

Can anyone help me to change Gregorian date to Persian in JavaScript? I want to use it in HTML and JavaScript.

like image 375
myname Avatar asked Feb 23 '16 07:02

myname


People also ask

How do I change a calendar to Persian?

To enable the Persian calendar, go into Settings, click on the General tab, and select it as an Alternate calendar.

How do you read dates in Farsi?

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.

What is today's date in Iranian calendar?

Today is Tuesday 18 Muharram (1) 1444 AH corresponding to 16 August - Ab (8) 2022 AD.

What is a Gregorian date format?

DD/MM/YYYY - European style Gregorian date format. YYYY/MM/DD - Sortable style Gregorian date format.


1 Answers

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));  
like image 100
Ali_Hr Avatar answered Sep 18 '22 14:09

Ali_Hr