Open In App

Moment.js Jalaali Calendar Plugin

moment-jalaali is a Moment.js plugin that allows us to use the moment.js library’s utilities and methods with the Jalaali calendar system. This calendar system is used in Persia and is still used in Iran and Afghanistan in different variants.

Installation of the plugin:



npm install moment-jalaali

Syntax:

const moment = require('moment-jalaali');
moment().format('jYYYY/jM/jD');

Note: When parsing a date, with a data-type string, pass a “j” to the format token. For example, for the year, use “jYYYY”, or for the month, use “jM”.



Example 1:




const moment = require('moment-jalaali');
 
// Parse a Jalaali date
const m = moment('1360/5/26', 'jYYYY/jM/jD')
 
// 1360/5/26 is 1981/8/17
console.log(m.format('jYYYY/jM/jD [is] YYYY/M/D'))

Output:

1360/5/26 is 1981/8/17

Example 2:




const moment = require('moment-jalaali');
 
// This returns true as it is leap year
console.log(moment('1391/12/30', 'jYYYY/jMM/jDD').isValid())
 
// This returns false as it is common year
console.log(moment('1392/12/30', 'jYYYY/jMM/jDD').isValid())

Output:

true 
false

Example 3:




const moment = require('moment-jalaali');
 
// 2013-8-25 16:40:00
console.log(moment('1392/6/3 16:40', 'jYYYY/jM/jD HH:mm')
    .format('YYYY-M-D HH:mm:ss'))
 
// 1392/6/31 23:59:59
console.log(moment('2013-8-25 16:40:00', 'YYYY-M-D HH:mm:ss')
    .endOf('jMonth').format('jYYYY/jM/jD HH:mm:ss'))

Output:

2013-8-25 16:40:00
1392/6/31 23:59:59

Example 4:




const moment = require('moment-jalaali');
 
// Represents 1981/07/17
console.log(moment('1981 5 17', 'YYYY jM D').format('YYYY/MM/DD'))

Output:

1981/07/17

Reference: https://momentjs.com/docs/#/plugins/jalaali/


Article Tags :