Click here to Skip to main content
15,900,725 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The problem is I want to create API using sequelize in that I want to find the financial year based on quarters.the financial year starts from April. There are a total of four quarters each quarter is 3 months. so their is 4 quarters like Q1, Q2, Q3 and Q4. the quarters i want like this 1-April-2030 to 30-june-2030 is Q1, 1-july-2023 to 30-september-2023 is Q2 so on. store all these quarters in a database table using seeder in sequlize.


What I have tried:

i'm created a model using sequelize -

const { Sequelize, DataTypes } = require("sequelize");

const quarter = sequelize.define(
  "quarter",
  {
    id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    name: {
      type: Sequelize.STRING,
      unique: true
    },
    startDate: {
      type: Sequelize.DATE,
      allowNull: false,
      defaultValue: Sequelize.NOW
    },
    endDate: {
      type: Sequelize.DATE,
      allowNull: false,
      defaultValue: Sequelize.NOW
    },
    updatedBy: {
      type: Sequelize.INTEGER,
      defaultValue: 1,
      references: {
        model: "users",
        key: "id"
      }
    }
  },
  {
    paranoid: true,
    deletedAt: "isDelete"
  }
);



This is the code i'm trying but not got below output:

function getQuarters(quarter) {
    const results = [];
    let startDate="",endDate="";
    for (let i=1;i<5;i++)  {

        results.push({
          name: "Q"+i,
          
           startDate: moment().quarter(quarter).startOf('quarter'),
           endDate: moment().quarter(quarter).endOf('quarter'),
          
        });
      startDate=endDate;
      endDate="";
    }
    return results;
  }

  let data = getQuarters(2);
  console.log(data);


The output i want is like this: 
Q1 start date: 1-April-2023 end date: 30-june-2023 Year-2023 
Q2 start date: 1-july-2023 end date: 30-september-2023 Year-2023 
Q3 start date: 1-october-2023 end date: 31-december-2023 Year-2023
Q4 start date: 1-january-2024 end date: 30-march-2024 Year-2024 
Q1 start date: 1-April-2024 end date: 30-june-2024 Year-2024 
Q2 start date: 1-july-2024 end date: 30-september-2024 Year-2024 
Q3 start date: 1-october-2024 end date: 31-december-2024 Year-2024 
Q4 start date: 1-january-2025 end date: 30-march-2025 Year-2025 so on.
Posted
Updated 17-Feb-23 3:04am
v3

1 solution

Using Sequelize, you can do the following. I re-wrote the code for easier implementation -
const { Op } = require("sequelize");

const Quarter = (sequelize, DataTypes) => {
  const Quarter = sequelize.define("Quarter", {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    startDate: {
      type: DataTypes.DATE,
      allowNull: false,
    },
    endDate: {
      type: DataTypes.DATE,
      allowNull: false,
    },
  });

  Quarter.calculateQuarters = async (startDate, endDate) => {
    const quarters = [];

    // Loop through each quarter and add it to the array
    for (let year = startDate.getFullYear(); year <= endDate.getFullYear(); year++) {
      for (let quarter = 1; quarter <= 4; quarter++) {
        // Calculate the start and end dates of the current quarter
        const quarterStartDate = new Date(year, (quarter - 1) * 3, 1);
        const quarterEndDate = new Date(year, (quarter * 3), 0);

        // Check if the quarter start date falls within the start and end dates
        if (quarterStartDate >= startDate && quarterStartDate <= endDate) {
          // Add the quarter to the array
          quarters.push({
            startDate: quarterStartDate,
            endDate: quarterEndDate,
          });
        }
      }
    }

    return quarters;
  };

  return Quarter;
};

module.exports = Quarter;


This should give you the output you require.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900