Skip to content

Instantly share code, notes, and snippets.

@canuk
Created February 24, 2014 19:26
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save canuk/9195138 to your computer and use it in GitHub Desktop.
Save canuk/9195138 to your computer and use it in GitHub Desktop.
Moment.js Fiscal Year Calculations (FY starts in October)
if (moment().quarter() == 4) {
var current_fiscal_year_start = moment().month('October').startOf('month');
var current_fiscal_year_end = moment().add('year', 1).month('September').endOf('month');
var last_fiscal_year_start = moment().subtract('year', 1).month('October').startOf('month');
var last_fiscal_year_end = moment().month('September').endOf('month');
} else {
var current_fiscal_year_start = moment().subtract('year', 1).month('October').startOf('month');
var current_fiscal_year_end = moment().month('September').endOf('month');
var last_fiscal_year_start = moment().subtract('year', 2).month('October').startOf('month');
var last_fiscal_year_end = moment().subtract('year', 1).month('September').endOf('month');
};
@canuk
Copy link
Author

canuk commented Feb 24, 2014

@mara-sanders
Copy link

Thanks!! Works Great!

@MrBitMaster
Copy link

const getFiscalYearTimestamps = () => {
    const startMonthName = "October";
    const endMonthName = "September";
    if (moment().quarter() == 4) {
      return {
        current: {
          start: moment().month(startMonthName).startOf('month'),
          end: moment().add(1, 'year').month(endMonthName).endOf('month')
        },
        last: {
          start: moment().subtract(1, 'year').month(startMonthName).startOf('month'),
          end: moment().month(endMonthName).endOf('month')
        }
      };
    } else {
      return {
        current: {
          start: moment().subtract(1, 'year').month(startMonthName).startOf('month'),
          end: moment().month(endMonthName).endOf('month')
        },
        last: {
          start: moment().subtract(2, 'year').month(startMonthName).startOf('month'),
          end: moment().subtract(1, 'year').month(endMonthName).endOf('month')
        }
      };
    }
  };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment