Open In App

Moment.js Hijri Calendar Plugin

Last Updated : 26 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn about moment-hijri, a moment.js plugin. 

What is moment-hijri?

moment-hijri is a Moment.js plugin that allows us to use the moment.js library’s utilities and methods with the Hijri calendar, a Muslim and Islamic Lunar calendar. The calendar conversions used by the plugin are based on Umm al-Qura calculations.

Installation of the plugin:

npm install moment-hijri

Syntax: 

const moment = require('moment-hijri');
moment().format('iYYYY/iM/iD');

When parsing a date, with a data-type string, pass an “i” to the format token. For example, for the year, use “iYYYY”, or for the month, use “iMM”.

Example 1: In this example, we will parse a Hijri date.

Javascript




const moment = require('moment-hijri');
  
// Parse a Hijri date
const m = moment('1410/8/28', 'iYYYY/iM/iD'); 
console.log(m.format('iYYYY/iM/iD [is] YYYY/M/D'));


Output:

 

Example 2: In this example, we are validating a Hijri date with our passed date.

Javascript




const moment = require('moment-hijri');
  
// False (This month is only 29 days)
console.log(moment('1436/1/30'
    'iYYYY/iMM/iDD').isValid());
      
// True (This month is 30 days)
console.log(moment('1436/2/30'
    'iYYYY/iMM/iDD').isValid());


Output:

 

Example 3: In this example, we are formatting a Hijri date in different formats.

Javascript




const moment = require('moment-hijri');
console.log(
    moment('1436/2/6 16:40'
    'iYYYY/iM/iD HH:mm').
    format('YYYY-M-D HH:mm:ss')
);
  
console.log(
    moment('2014-11-28 16:40:00'
    'YYYY-M-D HH:mm:ss').
    endOf('iMonth').format('iYYYY/iM/iD HH:mm:ss')
);


Output:

 

Example 4: An example of complex parsing of a Hijri date.

Javascript




const moment = require('moment-hijri');
console.log(moment('1990 5 25', 'YYYY iM D').
    format('YYYY/MM/DD'));


Output:

 

Reference: https://momentjscom.readthedocs.io/en/latest/moment/10-plugins/09-hijri/



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads