Skip to content
Related Articles

Related Articles

Moment.js Fiscal Quarters Plugin

View Discussion
Improve Article
Save Article
  • Last Updated : 17 Aug, 2022
View Discussion
Improve Article
Save Article

MomentJS is a JavaScript library that parses, validates, manipulates, and displays date/time in JavaScript in a very easy way. This chapter will provide an overview of MomentJS and discusses its features in detail. Moment JS allows displaying of date as per localization and in human-readable format.

The moment().fquarter() method is used to provide fiscal quarterly formatting for moment objects.

Syntax: 

moment("yyyy-mm-dd").fquarter();

Parameters: This method used along with fquarter() method that accepts single parameter in (“yyyy-mm-dd”) format as String. On moment(), fquarter() method is called.

  • String: It should be in format “yyyy-mm-dd”.

Return Value: moment().fquarter() returns object with following keys:

  • quarter: It contains a value in which the quarter given date lies. Its type is Number.
  • year: It contains a value in which year given date lies. Its type is Number.
  • nextYear: It contains the value what is next year for a given date. Its type is Number.
  • start: It contains the start date of that particular quarter for the given date. Its type is String.
  • end:  It contains the end date of that particular quarter for the given date. Its type is String.
  • toString: It converts returned objects in String readable format.

Setting up Node.js application:

Step 1: Create a Node.js application using the following command:

npm init

Step 2: After creating the NodeJS application, Install the required module using the following command:

npm install moment-fquarter 

Project Structure: The project structure will look like this:

 

Example 1: In this example, we will pass the date in a specific format to moment() and call fquarter() on moment(). We will get objects with some properties. Write down the below code in the app.js file.

app.js




const moment = require('moment-fquarter');
const quarterDetails = moment("2022-04-01").fquarter();
console.log(quarterDetails)

Steps to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

{
    quarter: 1,
    year: 2022,
     nextYear: 2023,
     start: '2022-04-01',
     end: '2022-06-30',
     toString: [Function (anonymous)]
}

Example 2: In this example, We will pass the count of July month which is 7 as a parameter to fquarter(). After converting an object into a string using toString(), we will get quarter number and fiscal year details.

app.js




const moment = require('moment-fquarter');
  
const quarterDetails = 
    moment("2022-01-01").fquarter(7).toString();
      
console.log('Quarter and year details - ',
    quarterDetails)

Steps to run the program: To run the application execute the below command from the root directory of the project:

node app.js

 Output:

Quarter and year details -  Q3 2021/22

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!