jalali-moment
A Jalali (Jalali, Persian, Khorshidi, Shamsi) calendar system plugin for moment.js. DEMO
Jalali calendar is a solar calendar. It gains approximately 1 day on the Julian calendar every 128 years. Read more on Wikipedia.
This plugin adds Jalali calendar support to momentjs library.
Calendar conversion is based on the algorithm provided by Kazimierz M. Borkowski and has a very good performance.
Where to use it
Like momentjs
, jalali-moment
works in browser and in Node.js.
Install
Install via NPM
npm install jalali-moment -S
Install via bower
bower install jalali-moment --save
Node.js
var moment = require('jalali-moment');
moment().format('jYYYY/jM/jD');
Angular
import * as moment from 'jalali-moment';
add a jalali 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>
Typescript
import * as moment from 'jalali-moment';
let todayJalali = moment().format('jYYYY/jM/jD');
ES5
<script src="thisRepository/jalali-moment/dist/jalali-moment.browser.js"></script>
<script>
moment().format('jYYYY/jM/jD');
</script>
Plunker
<script src='https://unpkg.com/jalali-moment/dist/jalali-moment.browser.js'></script>
<script>
moment().format('jYYYY/jM/jD');
</script>
Plunker and Typescript
You could use systemjs to import this library into your project like this
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:
m = moment('1367/11/4', 'jYYYY/jM/jD');
m.format('jYYYY/jM/jD [is] YYYY/M/D');
m.jDayOfYear();
m.jWeek();
m.jWeekYear();
moment.jIsLeapYear(m.jYear());
m.jYear(1368);
m.jMonth();
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");
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');
for more information about api you could read moment.js.
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');
Just use jalali calendar system
We could use both calendar system concurrently but
sometimes we just need jalali system or we wouldn't like to change all moment.js methods and formats to work in jalali system.
for example when you want to edit a datepicker (which is written by using moment.js) to work in jalali calendar system.
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');
Related Projects
jalali-angular-datepicker ( angular2 or more)
A highly configurable date picker built for Angular 2 applications using jalali-moment
is fingerpich/jalali-angular-datepicker created by @Fingerpich.
License
MIT