Reputation: 426
I'm working on a loan management system using NodeJS for the Back End and AngularJS for the front end. Simply this is what I need to do.
There are 3 categories in the system.
Loan amount is $5000
and this is a Weekly loan
. It mean customer need to pay this loan weekly basis.
Start date is 2018-12-11 ( December 11 , 2018)
End date is 2019-05-11 (April 11 , 2019)
Interest rate is 8%
.
What I want to do is , find all the weeks between start date and end date. And also today is Tuesday and in every Tuesday of week, customer need to do a payment. So I need to save that repayments list when adding a new loan.
Simply I want to count 7 days from 2018-12-11 till 2019-05-11. And specially we need to skip holidays. It's being saved in the system. When we are calculating the dates, we can check that from holiday list and skip them.
This is a sample output I want :
Date : 2018-12-11 , Amount : $xx
Date : 2018-12-18 , Amount : $xx
Date : 2018-12-25 , Amount : $xx
Date : 2019-01-01 , Amount : $xx
Date : 2019-01-08 , Amount : $xx
Date : 2019-01-15 , Amount : $xx . . .
What I have tried some far.
I tried to use https://momentjs.com/
and tried to get weeks like this :
moment().add(10, 'days').calendar();
-> From this we can add all the dates and get the date after 10 days.
And also I used https://github.com/datejs/Datejs
for this and still didn't get a way to loop all the dates.
Upvotes: 1
Views: 683
Reputation: 12542
You can use moment
module in NodeJS.
npm i moment
It has a function .diff
, from the docs:
//moment().diff(Moment|String|Number|Date|Array);
//moment().diff(Moment|String|Number|Date|Array, String);
//moment().diff(Moment|String|Number|Date|Array, String, Boolean);
var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days') // 1
EDIT:
So basically, You want to +7 days excluding holidays.
Below is a very basic loop based solution. But there are libraries doing what you want.
const start = moment().startOf('day');
const end = moment(somedate).startOf('day');
let current = start;
const holidayList = [[2018, 11, 25]]
while(current.isSameOrBefore(end)){
let nextWeek = moment(current).add(7, 'days');
//check if holidays are between nextWeek and current
//get number of holidays
const number_of_holidays = holidayList.reduce((p,c)=>{
if(moment(c).isAfter(current) && moment(c).isSameOrBefore(nextWeek)){
return p+1;
}
return p;
},0)
nextWeek = nextWeek.add(number_of_holidays, 'days')
//do something
console.log('---'+nextWeek.toDate());
current = nextWeek;
}
You can checkout momentjs-business or moment-weekday-calc.
Edit 2: From the github issue I can see that you only need Tuesdays, without the holiday.
const end = moment(endDate).startOf('day');
const holidayList = [[2018, 11, 25]]
const dayINeed = 2; //for tuesday
let current = moment().isoWeekday();
if (current <= dayINeed) {
current = moment().isoWeekday(dayINeed).startOf('day');
} else {
current = moment().add(1, 'weeks').isoWeekday(dayINeed).startOf('day');
}
while(current.isSameOrBefore(end)){
/* HERE check for holiday */
console.log(current.toArray());
current.add(1, 'weeks');
}
Upvotes: 2