Skip to content

Instantly share code, notes, and snippets.

@3gcodes
Last active September 23, 2020 18:56
Show Gist options
  • Save 3gcodes/84224e9640bdd2f0cb76b5e769364eec to your computer and use it in GitHub Desktop.
Save 3gcodes/84224e9640bdd2f0cb76b5e769364eec to your computer and use it in GitHub Desktop.
/*
add your bills to the bills array.
repeat: once | weekly | bi-weeky | monthly | bi-monthly
if using bi-monthly, you'll need a first and second argument
see examples below
*/
var bills = [
{"name": "Some Bill", "amount": -672, "next": new Date("2020-09-25"), repeat: "monthly"},
{"name": "Income", "amount": 2000.00, "next": new Date("2020-09-25"), repeat: "weekly"},
{"name": "Loan", "amount": -291.70, "next": new Date("2020-09-28"), repeat: "bi-monthly", first: 9, second: 28},
{"name": "More Income", "amount": 1049.50, "next": new Date("2020-10-02"), repeat: "bi-weekly"}
];
// put your current balance here
const current = 748.09;
// put today's date here. The String is important so that it does the time right.
const today = new Date("2020-09-23");
// this is how far out your want to project.
const projection = new Date("2020-10-01");
var projectedBalance = current;
bills.sort((a, b) => (a.day > b.day) ? 1: -1);
console.log("Projected Balance: of " + projectedBalance + " on " + today.toISOString().slice(0, 19).replace(/-/g, "/").replace("T", " "));
for (var d = today; d < projection; d.setDate(d.getDate() + 1)) {
const billsFound = bills.filter(bill => (bill.next.getMonth() === d.getMonth() && bill.next.getDate() === d.getDate()));
if (billsFound.length > 0) {
for (var i = 0; i < billsFound.length; i++) {
const bill = billsFound[i];
console.log(bill.name + " bill found on " + d.toISOString().slice(0, 19).replace(/-/g, "/").replace("T", " ") + " for $" + bill.amount);
projectedBalance += bill.amount;
console.log("Projected Balance: of $" + projectedBalance + " on " + d.toISOString().slice(0, 19).replace(/-/g, "/").replace("T", " "));
const repeat = bill.repeat;
switch(repeat) {
case "once":
break;
case "monthly":
bill.next = new Date(bill.next.setMonth(bill.next.getMonth() + 1));
break;
case "weekly":
bill.next = new Date(bill.next.setDate(bill.next.getDate() + 7));
break;
case "bi-weekly":
bill.next = new Date(bill.next.setDate(bill.next.getDate() + 14));
break;
case "bi-monthly":
if (bill.first == d.getDate()+1) {
bill.next = new Date(bill.next.setDate(bill.second));
} else if (bill.second == d.getDate()+1) {
const year = d.getFullYear();
var first = bill.first.toString();
var month = (bill.next.getMonth() + 1).toString();
if (year === 11) year = 0;
if (month.length == 1) month = "0" + month;
if (first.length == 1) first = "0" + first;
bill.next = new Date(year.toString() + "-" + month + "-" + first);
}
break;
default:
break;
}
}
}
}
console.log("Projected Balance: of " + projectedBalance);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment