moment-jalaali
A Jalaali (Jalali, Persian, Khorshidi, Shamsi) calendar system plugin for moment.js.
Jalali calendar is a solar calendar that was used in Persia, variants of which today are still in use in Iran as well as Afghanistan. Read more on Wikipedia or see Calendar Converter.
This plugin adds Jalaali calendar support to momentjs library.
Calendar conversion is based on the algorithm provided by Kazimierz M. Borkowski and has a very good performance.
Note (Feb 2022)
If you just need to display date and time in Persian calendar, you may use Intl
which is ECMAScript Internationalization API with a very good browser support. For example:
const d = new Date(2022,2,21)
console.log(new Intl.DateTimeFormat('fa-IR').format(d));
console.log(new Intl.DateTimeFormat('fa-IR', {dateStyle: 'full', timeStyle: 'long'}).format(d));
console.log(new Intl.DateTimeFormat('fa-IR-u-nu-latn', {dateStyle: 'full', timeStyle: 'long'}).format(d));
console.log(new Intl.DateTimeFormat('en-US-u-ca-persian', {dateStyle: 'full', timeStyle: 'long'}).format(d));
console.log(new Intl.DateTimeFormat('en-US-u-ca-persian', {year: 'numeric'}).format(d));
console.log(new Intl.DateTimeFormat('en-US-u-ca-persian', {month: 'short'}).format(d));
console.log(new Intl.DateTimeFormat('en-US-u-ca-persian', {day: 'numeric'}).format(d));
Where to use it
Like momentjs
, moment-jalaali
works in browser and in Node.js.
Node.js
npm install moment-jalaali
var moment = require('moment-jalaali')
moment().format('jYYYY/jM/jD')
Browser
You may use the node_modules/build/moment-jalaali.js
file.
<script src="node_modules/moment/min/moment.min.js"></script>
<script src="node_modules/moment-jalaali/build/moment-jalaali.js"></script>
<script>
moment().format('jYYYY/jM/jD')
</script>
Typescript
If you need type definitions for typescript, you need to install them separately.
npm install @types/moment-jalaali
API
This plugin tries to mimic momentjs
api. Basically, when you want to format or parse a string, just add a j
to the format token like 'jYYYY' or 'jM'. For example:
m = moment('1360/5/26', 'jYYYY/jM/jD')
m.format('jYYYY/jM/jD [is] YYYY/M/D')
m.jYear()
m.jMonth()
m.jDate()
m.jDayOfYear()
m.jWeek()
m.jWeekYear()
m.add(1, 'jYear')
m.add(2, 'jMonth')
m.add(3, 'day')
m.format('jYYYY/jM/jD')
m.jMonth(11)
m.startOf('jMonth')
m.format('jYYYY/jM/jD')
m.jYear(1392)
m.startOf('jYear')
m.format('jYYYY/jM/jD')
m.subtract(1, 'jYear')
m.subtract(1, 'jMonth')
m.format('jYYYY/jM/jD')
moment('1391/12/30', 'jYYYY/jMM/jDD').isValid()
moment('1392/12/30', 'jYYYY/jMM/jDD').isValid()
moment.jIsLeapYear(1391)
moment.jIsLeapYear(1392)
moment.jDaysInMonth(1395, 11)
moment.jDaysInMonth(1394, 11)
moment('1392/6/3 16:40', 'jYYYY/jM/jD HH:mm').format('YYYY-M-D HH:mm:ss')
moment('2013-8-25 16:40:00', 'YYYY-M-D HH:mm:ss').endOf('jMonth').format('jYYYY/jM/jD HH:mm:ss')
moment('1981 5 17', 'YYYY jM D').format('YYYY/MM/DD')
To add Persian language, use loadPersian method:
moment.loadPersian([options])
Options
Param | Type | Default | Description | Example |
---|
usePersianDigits | Boolean | false | Use persian digits (Use at your own risk) | moment.loadPersian({usePersianDigits: true}) |
dialect * | String | persian | Available values = persian , persian-modern | moment.loadPersian({dialect: 'persian-modern'}) |
*use dialect option to change usePersian
dialect, available options are:
in case getting error 'humanize' of undefined
you should also require locale fa
require('moment/locale/fa')
React Native
To use fromNow()
in React Native projects:
import moment from "moment-jalaali";
import fa from "moment/src/locale/fa";
moment.locale("fa", fa);
moment.loadPersian();
Related Projects
react-datepicker2
A simple and reusable Datepicker component for React (with persian jalali calendar support) Demo.
created by @mberneti.
react-advance-jalaali-datepicker
A React module, which provides different jalaali (persian) datepicker types such as, range date and time picker, developed base on moment-jalaali
. react-advance-jalaali-datepicker created by @A-Kasaaian.
ng-persian-datepicker
ng-persian-datepicker is an angular 8+ date-time picker component for shamsi calendar system with some useful customization settings.
ng-jalali-flat-datepicker
A lightweight angular.js date picker using moment-jalaali
is thg303/ng-jalali-flat-datepicker created by @thg303.
pholiday
A library based on moment-jalaali
for calculating holidays in Persian calendar is shkarimpour/pholiday created by @shkarimpour.
moment-hijri
Another calendar system plugin for momentjs
is moment-hijri created by @xsoh.
vue-persian-datetime-picker
vue-persian-datetime-picker is a vuejs
plugin to select jalali date and time, created by @talkhabi.
react-native-general-calendars
react-native-general-calendars is a react-native
component with support of gregorian, jalaali and hijri calendar to selectand work with date and time, created by @rghorbani.
imrc-datetime-picker
imrc-datetime-picker is a react
component with support of gregorian and jalaali calendar in both english and persian (modern) languages to select and work with date and time, created by @smrsan76.
License
MIT