mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 21:00:31 +00:00
feat(nestjs): migrate to NestJS
This commit is contained in:
57
packages/server/src/utils/date-range-collection.ts
Normal file
57
packages/server/src/utils/date-range-collection.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
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;
|
||||
};
|
||||
Reference in New Issue
Block a user