Moment.js Islamic Civil Calendar Plugin
Moment-islamic-civil 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 civil calculations. In this article, we will learn about moment-islamic-civil, a moment.js plugin.
Syntax
const moment = require('@whitewater/moment-islamic-civil');
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 “iM”.
Parameters: It takes two strings as optional parameters. The first string is the date that you want to parse, and the second string is the format in which you want to parse the date.
Return value: This API returns a moment object.
Installation plugin
npm install github:ACGC/moment-islamic-civil
Example 1: Parsing a Hijri date
Javascript
const moment = require( '@whitewater/moment-islamic-civil' );
const m = moment( '1210/7/28' , 'iYYYY/iM/iD' );
console.log(m.format( 'iYYYY/iM/iD [is] YYYY/M/D' ));
|
Output:
Example 2: Validating a Hijri date with our passed date.
Javascript
const moment = require( '@whitewater/moment-islamic-civil' );
console.log(moment( '1536/1/30' , 'iYYYY/iMM/iDD' ).isValid());
console.log(moment( '1536/2/30' , 'iYYYY/iMM/iDD' ).isValid());
|
Output:
Example 3: Formatting a Hijri date in different formats
Javascript
const moment = require( '@whitewater/moment-islamic-civil' );
console.log(moment( '1536/2/6 16:40' , 'iYYYY/iM/iD HH:mm' )
.format( 'YYYY-M-D HH:mm:ss' ));
console.log(moment( '2014-10-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( '@whitewater/moment-islamic-civil' );
console.log(moment( '1890 5 20' , 'YYYY iM D' )
.format( 'YYYY/MM/DD' ));
|
Output:
Reference: https://momentjscom.readthedocs.io/en/latest/moment/10-plugins/10-islamic-civil/
Last Updated :
30 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...