TL;DR, The requirement is to be able to take a Persian (Jalali) date (also known as Persian Solar Hijri Calendar) like Esfand 19, 1400
(i.e. "12/19/1400"
and convert it to other calendars (Gregorian, Islamic, Chinese, Hebrew, etc.) without using external libraries or complex Astronomical Equations. And without using the pending implementation of the new date Temporal API into Javascript.
Javascript built-in method Intl.DateTimeFormat()
converts Gregorian Dates into various calendars' dates (18 World Calendars) including the formatting of the output string.
However, as of today (March 2022), Javascript does not provide a built-in method for the reverse operation, i.e. converting the Persian Dates (and other calendars' dates) back into Gregorian Dates or into other calendars. For such purposes, you will need to use External Date Libraries to do the conversion such as 'moment.js' and many others.
My method of doing the date conversion follows as an answer to this question as recommended by StackOverflow here: Can I answer my own question?
The short Javascript function below does not use external libraries and provides the facilities to convert a Persian (Jalali) Dates (from Persian year -272,442 AP to +275,139 AP) into any of the following 18 Javascript Calendars with options for formatting the resulting output:
"buddhist", "chinese", "coptic", "dangi", "ethioaa", "ethiopic", "gregory", "hebrew", "indian", "islamic", "islamic-umalqura", "islamic-tbla", "islamic-civil", "islamic-rgsa", "iso8601", "japanese", "persian", "roc", "islamicc".
The method, also, does not use complex mathematical or astronomical formulas and relies solely on the Javascript built-in calendar conversion algorithms which are in turn based on the ICU code [https://icu.unicode.org/].
This approach ensures that the output is always accurate and fully compliant with the Javascript engine output.
Syntax
In its simplest form, the function defaults to converting the Persian Date into the
Gregorian
calendar using the ISO Date Format.Example: Convert the Persian Date Esfand 19, 1400 (i.e. 12/19/1400) to Gregorian.
To to convert Persian Date to another calendar (say 'Islamic' calendar):
To add formatting to the output, use the
'dateStyle'
options as in the JavascriptIntl.DateTimeFormat()
method.Example: Convert Persian Date to Islamic Date with full dateStyle
Example: Convert a Persian Date into Hebrew with Persian Locale
The above can be done for all other 18 Calendars.
An added feature is the ability to format the Persian Date into any of the available
'dateStyles'
and'locales'
without conversion.To do that specify the
'toCal'
topersian
Example: Use Persian Locale to Format a Persian Date
Example: Format a Persian Date in the Hindi Locale
You can use all the options available in the
Intl.DateTimeFormat()
for formatting the output date.Invalid Persian Dates
If an invalid Persian Date is passed to the function an error
Invalid Persian Date!
will be generated.Invalid Persian Dates are dates that have incorrect days in the month or incorrect days or months.
For example, the Persian Date 1400/12/30 is invalid because month 12 of the Persian Calendar (month "Esfand") is 29 days in the year 1400.
The future Javascript Temporal API will make this task simpler.