jalali-moment
It adds jalali (Persian, Khorshidi, Shamsi) calendar system to moment.js library.
DEMO
jalali calendar is a solar calendar. 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.
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 and following code will work
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
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>
Using in Plunker
####ES5
<script src='https://unpkg.com/jalali-moment/dist/jalali-moment.browser.js'></script>
<script>
moment().format('jYYYY/jM/jD');
</script>
####Typescript or es6
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:
now = moment();
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(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');
all features are documented in here.
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 could use Jalali calendar as default system like the following code:
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');
For example:
to make a datepicker work with jalali calendar system you could use this feature.
Related Projects
jalaali-moment
A Jalaali calendar system plugin for moment.js.
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.
License
MIT