Last active
October 18, 2021 10:36
-
-
Save namnamir/772555ce0a5f6da757bbb209f86c384f to your computer and use it in GitHub Desktop.
This function gets a date and convert it into a Jalali (Persian) date including all possible formats. It also lets the user to convert Farsi digits to Latin ones.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString | |
// options: | |
// weekday -> [long | short | narrow] | |
// era -> [long | short | narrow] | |
// timeZoneName -> [long | short] | |
// | |
// year -> [numeric | 2-digit] | |
// month -> [numeric | 2-digit | long | short | narrow] | |
// day -> [numeric | 2-digit] | |
// hour -> [numeric | 2-digit] | |
// minute -> [numeric | 2-digit] | |
// second -> [numeric | 2-digit] | |
// to convert digits to Latin digits | |
// .replace(/([۰-۹])/g, token => String.fromCharCode(token.charCodeAt(0) - 1728)); | |
function PersianDate(raw_date) { | |
if (raw_date == '') { | |
return '' | |
} | |
date = new Date(raw_date); | |
json_date = { | |
'weekday': { | |
'long': date.toLocaleDateString('fa-IR', { weekday: 'long' }), | |
'short': date.toLocaleDateString('fa-IR', { weekday: 'short' }), | |
'narrow': date.toLocaleDateString('fa-IR', { weekday: 'narrow' }), | |
}, | |
'era': { | |
'long': date.toLocaleDateString('fa-IR', { era: 'long' }), | |
'short': date.toLocaleDateString('fa-IR', { era: 'short' }), | |
'narrow': date.toLocaleDateString('fa-IR', { era: 'narrow' }), | |
}, | |
'timeZoneName': { | |
'long': date.toLocaleDateString('fa-IR', { timeZoneName: 'long' }), | |
'short': date.toLocaleDateString('fa-IR', { timeZoneName: 'short' }), | |
}, | |
'year': { | |
'numeric': date.toLocaleDateString('fa-IR', { year: 'numeric' }), | |
'2-digit': date.toLocaleDateString('fa-IR', { year: '2-digit' }), | |
}, | |
'month': { | |
'numeric': date.toLocaleDateString('fa-IR', { month: 'numeric' }), | |
'2-digit': date.toLocaleDateString('fa-IR', { month: '2-digit' }), | |
'long': date.toLocaleDateString('fa-IR', { month: 'long' }), | |
'short': date.toLocaleDateString('fa-IR', { month: 'short' }), | |
'narrow': date.toLocaleDateString('fa-IR', { month: 'narrow' }), | |
}, | |
'day': { | |
'numeric': date.toLocaleDateString('fa-IR', { day: 'numeric' }), | |
'2-digit': date.toLocaleDateString('fa-IR', { day: '2-digit' }), | |
}, | |
'hour': { | |
'numeric': date.toLocaleDateString('fa-IR', { hour: 'numeric' }), | |
'2-digit': date.toLocaleDateString('fa-IR', { hour: '2-digit' }), | |
}, | |
'minute': { | |
'numeric': date.toLocaleDateString('fa-IR', { minute: 'numeric' }), | |
'2-digit': date.toLocaleDateString('fa-IR', { minute: '2-digit' }), | |
}, | |
'second': { | |
'numeric': date.toLocaleDateString('fa-IR', { second: 'numeric' }), | |
'2-digit': date.toLocaleDateString('fa-IR', { second: '2-digit' }), | |
} | |
} | |
return json_date; | |
} | |
// examples: | |
// | |
// 1- Customize the output format | |
// | |
// date = PersianDate(new Date()) | |
// console.log(date['weekday']['narrow'] + ' ' + date['day']['2-digit'] + ' ' + date['month']['long'] + ' ' + date['year']['numeric']); | |
// output: | |
// ی ۱۰ خرداد ۱۴۰۰ | |
// | |
// 2- Get the all possible option in a JSON format | |
// | |
// PersianDate(new Date()) | |
// output: | |
// { | |
// "weekday": { | |
// "long": "یکشنبه", | |
// "short": "یکشنبه", | |
// "narrow": "ی" | |
// }, | |
// "era": { | |
// "long": "هجری شمسی ۱۴۰۰ ۷ ۲۵", | |
// "short": "ه.ش. ۱۴۰۰ ۷ ۲۵", | |
// "narrow": "ه.ش. ۱۴۰۰ ۷ ۲۵" | |
// }, | |
// "timeZoneName": { | |
// "long": "۱۴۰۰/۷/۲۵، وقت تابستانی مرکز اروپا", | |
// "short": "۱۴۰۰/۷/۲۵، +۲ گرینویچ" | |
// }, | |
// "year": { | |
// "numeric": "۱۴۰۰", | |
// "2-digit": "۰۰" | |
// }, | |
// "month": { | |
// "numeric": "۷", | |
// "2-digit": "۰۷", | |
// "long": "مهر", | |
// "short": "مهر", | |
// "narrow": "م" | |
// }, | |
// "day": { | |
// "numeric": "۲۵", | |
// "2-digit": "۲۵" | |
// }, | |
// "hour": { | |
// "numeric": "۱۴۰۰/۷/۲۵، ۲۲", | |
// "2-digit": "۱۴۰۰/۷/۲۵، ۲۲" | |
// }, | |
// "minute": { | |
// "numeric": "۱۴۰۰/۷/۲۵، ۵۷", | |
// "2-digit": "۱۴۰۰/۷/۲۵، ۵۷" | |
// }, | |
// "second": { | |
// "numeric": "۱۴۰۰/۷/۲۵، ۳۹", | |
// "2-digit": "۱۴۰۰/۷/۲۵، ۳۹" | |
// } | |
// } | |
// | |
// 3- Replatece Farsi digits with Latin ones, e.g. ۲۵ -> 25 | |
// | |
// date = PersianDate(new Date()) | |
// console.log(date['day']['2-digit'].replace(/([۰-۹])/g, token => String.fromCharCode(token.charCodeAt(0) - 1728))); | |
// output: | |
// 25 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment