mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-15 20:30:33 +00:00
58 lines
1.4 KiB
TypeScript
58 lines
1.4 KiB
TypeScript
import * as moment from 'moment';
|
|
|
|
export const dateRangeCollection = (
|
|
fromDate,
|
|
toDate,
|
|
addType: moment.unitOfTime.StartOf = 'day',
|
|
increment: number = 1,
|
|
) => {
|
|
const collection = [];
|
|
const momentFromDate = moment(fromDate);
|
|
let dateFormat = '';
|
|
|
|
switch (addType) {
|
|
case 'day':
|
|
default:
|
|
dateFormat = 'YYYY-MM-DD';
|
|
break;
|
|
case 'month':
|
|
case 'quarter':
|
|
dateFormat = 'YYYY-MM';
|
|
break;
|
|
case 'year':
|
|
dateFormat = 'YYYY';
|
|
break;
|
|
}
|
|
for (
|
|
let i = momentFromDate;
|
|
i.isBefore(toDate, addType) || i.isSame(toDate, addType);
|
|
i.add(increment, `${addType}s` as moment.unitOfTime.DurationConstructor)
|
|
) {
|
|
collection.push(i.endOf(addType).format(dateFormat));
|
|
}
|
|
return collection;
|
|
};
|
|
|
|
export const dateRangeFromToCollection = (
|
|
fromDate: moment.MomentInput,
|
|
toDate: moment.MomentInput,
|
|
addType: moment.unitOfTime.StartOf = 'day',
|
|
increment: number = 1,
|
|
) => {
|
|
const collection = [];
|
|
const momentFromDate = moment(fromDate);
|
|
const dateFormat = 'YYYY-MM-DD';
|
|
|
|
for (
|
|
let i = momentFromDate;
|
|
i.isBefore(toDate, addType) || i.isSame(toDate, addType);
|
|
i.add(increment, `${addType}s` as moment.unitOfTime.DurationConstructor)
|
|
) {
|
|
collection.push({
|
|
fromDate: i.startOf(addType).format(dateFormat),
|
|
toDate: i.endOf(addType).format(dateFormat),
|
|
});
|
|
}
|
|
return collection;
|
|
};
|