Jalali Moment
Display, parse, manipulate and validate jalali (Persian, Khorshidi, Shamsi) or Gregorian (Miladi) dates and times and also
convert Jalali (Persian, Khorshidi, Shamsi) date to Gregorian (Miladi) or vice versa in javascript or typescript.DEMO
Read this in other languages: فارسی
Introduction
jalali(Persian) calendar is a solar calendar system. It gains approximately 1 day on the Julian calendar every 128 years. Read more on Wikipedia.
Calendar conversion is based on the algorithm provided by Kazimierz M. Borkowski and has a very good performance.
This plugin adds Jalali (Persian, Khorshidi, Shamsi) calendar system to moment.js library.
Install
Install via npm
npm install jalali-moment -S
Install via yarn
yarn add jalali-moment
Install via bower
bower install jalali-moment --save
Using in Node.js
Install it via npm or yarn then use it as the following code
var moment = require('jalali-moment');
moment().format('jYYYY/jM/jD');
Using in browser
ES5
<script src="thisRepositoryPath/dist/jalali-moment.browser.js"></script>
<script>
moment().format('jYYYY/jM/jD');
</script>
Typescript
import * as moment from 'jalali-moment';
let todayJalali = moment().format('jYYYY/jM/jD');
Angular 2 or 4
import * as moment from 'jalali-moment';
Add a pipe
@Pipe({
name: 'jalali'
})
export class JalaliPipe implements PipeTransform {
transform(value: any, args?: any): any {
let MomentDate = moment(value);
return MomentDate.format("jYYYY/jM/jD");
}
}
and use it in component template
<div>{{ loadedData.date | jalali }}</div>
API
This plugin tries to mimic moment.js 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:
now = moment();
Parse
Create a instance of moment from a Jalali (Persian) or Miladi date and time as string.more
m = moment('1367/11/4', 'jYYYY/jM/jD');
m = moment('1989/1/24', 'YYYY/M/D');
Display jalali or miladi date
Display moment instance as a string.more
m.format('jYYYY/jM/jD');
m.format('jMM');
m.format('M');
m.format('MM');
m.format('MMMM');
m.format('jMMMM');
m.format('jDD');
m.format('jYYYY/jM/jD [is] YYYY/M/D');
m.jDayOfYear();
m.jWeek();
m.jWeekYear();
Manipulate
There are a number of methods to modify date and time.more
m.jYear(1368);
m.jMonth(3);
m.jDate(10);
m.format("jYYYY/jMM/jD");
m.subtract(1, "jyear");
m.format("jYYYY/jMM/jD");
m.add(2, "jmonth");
m.format("jYYYY/jMM/jD");
m.endOf('jMonth').format("jYYYY/jMM/jD");
m.startOf('jYear').format("jYYYY/jMM/jD");
Validate
Check a date and time.more
m = moment('1367/11/4', 'jYYYY/jM/jD');
m.jIsLeapYear();
m.isLeapYear();
m.isSame('1989-01-01', 'year');
m.isSame(moment('1367-01-01','jYYYY-MM-DD'), 'jyear');
m.isBefore(moment('1367-01-01','jYYYY-MM-DD'), 'jyear');
m.isAfter(moment('1367-01-01','jYYYY-MM-DD'), 'jyear');
m.isValid();
moment('1396/7/31','jYYYY/jM/jD').isValid();
validation demo in plunker
Convert persian(Jalali , Shamsi, khorshidi) to gregorian (miladi) calendar system
moment('1392/6/3 16:40', 'jYYYY/jM/jD HH:mm')
.format('YYYY-M-D HH:mm:ss');
Convert gregorian (miladi) to jalali (Shamsi, persian)
moment('2013-8-25 16:40:00', 'YYYY-M-D HH:mm:ss')
.format('jYYYY/jM/jD HH:mm:ss');
Load Persian
To add Persian language, use loadPersian method:
moment().format('jYYYY/jMMMM/jD');
moment.loadPersian();
moment().format('jYYYY/jMMMM/jD');
moment.loadPersian(true);
moment().format('jYYYY/jMMMM/jD');
moment.unloadPersian();
moment().format('jYYYY/jMMMM/jD');
Use jalali calendar system primarily
This plugin adds Jalali calendar system to moment.js which is using gregorian calendar system
so we could use both calendar system concurrently but
you can use Jalali calendar as default system.
Set it for an instance
m.doAsJalali(true);
m.subtract(1,'year').format('YYYY/MMMM/D');
m.doAsGregorian();
as you see you don't need to change YYYY
to jYYYY
or use jyear
instead of year
Set it globally for all moment instances
moment().format('YYYY/MMMM/D');
moment.useJalaliSystemPrimarily();
moment().format('YYYY/MMMM/D');
moment().subtract(1,'year').format('YYYY/MMMM/D');
moment().subtract(1,'month').format('YYYY/MMMM/D');
moment.useJalaliSystemSecondary();
moment().format('YYYY/MMMM/D');
Change calendar system on changing its locale
moment.bindCalendarSystemAndLocale();
An example usage:
To make a datePicker work with jalali(shamsi) calendar system you could use this ability.
Using in Plunker
ES5
<script src='https://unpkg.com/jalali-moment/dist/jalali-moment.browser.js'></script>
<script>
moment().format('jYYYY/jM/jD');
</script>
es5 demo in plunker
Typescript or es6
You could use systemjs to import this library into your project like this
Related Projects
jalali-angular-datepicker ( angular2 or more)
A highly configurable date picker built for Angular 4 or Angular 2 applications using jalali-moment
is fingerpich/jalali-angular-datepicker created by @Fingerpich.
jalaali-moment
A Jalaali calendar system plugin for moment.js is jalaali-moment.
License
MIT