-
Notifications
You must be signed in to change notification settings - Fork 13
/
moment-fquarter.js
73 lines (59 loc) · 2.02 KB
/
moment-fquarter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// moment-fquarter.js
// version : 1.0
// author : Rob Allen
// license : MIT
// github.com/robgallen/moment-fquarter
(function () {
function onload(moment) {
moment.fn.fquarter = function (startMonth) {
startMonth = startMonth || 4; // default is April
var thisDate = this.clone();
var initial = thisDate.local()._quarter || "Q";
var result = {};
var adjustedDate = thisDate;
var nextYear = null;
var fiscalCalendarIndex = buildFiscalCalendar(startMonth).indexOf(this.month());
var monthsIntoQuarter = fiscalCalendarIndex % 3;
var startOfQuarter = this.clone().startOf("month").subtract(monthsIntoQuarter, "months");
if (startMonth > 1) {
adjustedDate = thisDate.subtract(startMonth - 1, "months");
nextYear = adjustedDate.clone().add(1, "years");
}
if (startMonth < 0) {
adjustedDate = thisDate.subtract(12 + startMonth, "month").add(1, "year");
nextYear = adjustedDate.clone().add(1, "year");
}
result.quarter = Math.ceil((adjustedDate.month() + 1.0) / 3.0);
result.year = adjustedDate.year();
result.nextYear = (nextYear) ? nextYear.year() : nextYear;
result.start = startOfQuarter.format("YYYY-MM-DD");
result.end = startOfQuarter.add(3, "months").subtract(1, 'day').format("YYYY-MM-DD");
result.toString = function () {
var str = initial + result.quarter + " " + result.year;
return (nextYear) ? str + "/" + nextYear.format("YY") : str;
};
return result;
};
return moment;
}
function buildFiscalCalendar(startMonth) {
if (startMonth < 0) {
startMonth = 13 + startMonth;
}
startMonth--;
var months = [];
for (var i = 0; i < 12; i++) {
months.push(startMonth);
startMonth++;
if (startMonth > 11) startMonth = 0;
}
return months;
}
if (typeof define === "function" && define.amd) {
define("moment-fquarter", ["moment"], onload);
} else if (typeof module !== "undefined") {
module.exports = onload(require("moment"));
} else if (typeof window !== "undefined" && window.moment) {
onload(window.moment);
}
}).apply(this);