Javscript Small savings calculator

01 January 2020 — Written by Mitesh Patel
#savings#javascript#investment

The script is created purely based the video I saw on youtube about Personal finance on how to save money with small savings.

If you invest 2 INR a day,In a year you can save around 133590 INR.

The calculation goes like: if on the, 1st of january you save 2 rupees 2nd january you have to save 22 = 4 and 3 day 23 = 6 and likewise.

You can not ideally transac this small amount to the bank, so I created this script to calculate monthly savings, so you can put that amount aside for any given month.

/**
* The script is created purely based the video I saw on youtube about
* Personal finance on how to save money with small savings.
*
* If you invest 2 INR a day,In a year you can save around 133590 INR.
*
* The calculation goes like if on the 1st of january you save 2 rupees
* 2nd january you have to save 2*2 = 4 and 3 day 2*3 = 6 and likewise.
*
* You can not ideally transac this small amount to the bank, so I created
* this script to calculate monthly savings, so you can put that amount aside
* for any given month.
*/
/**
* Amount to pay on any given day.
*
* This will give you the money that you have to save on the 70th day of the
* year and it will return the today's saving and total saving till the given
* day
*
* @param {*} day
* @param {*} dailyInvest
*/
function amountToPay(day, dailyInvest = 2) {
let currentInvest = 0;
let amount = 0;
for (let i = 1; i <= day; i++) {
currentInvest = i * dailyInvest;
amount = amount + currentInvest;
}
return {
today: currentInvest,
total: amount
};
}
/**
* Amount to pay in a month.
*
* Pass the month and a year and this will given you how money you
* Need to invest for the given month.
*
* @param {*} month
* @param {*} year
*/
function amountToPayInMonth(month, year) {
const { firstDay, lastDay } = daysNumberOfMonth(month, year);
const { total: firstDayPay } = amountToPay(getDayOfTheYear(firstDay));
const { total: lastDayPay } = amountToPay(getDayOfTheYear(lastDay));
// console.log(firstDayPay);
return lastDayPay - firstDayPay;
}
/**
* First and last day of the month.
*
* @param {*} month
* @param {*} year
*/
function daysNumberOfMonth(month, year) {
var date = new Date(year + "-" + month + "-01");
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
return {
firstDay: firstDay,
lastDay: lastDay
};
}
/**
* Get the number of day of the given date.
*
* returns the number of the day in a year, if you want to know
* what's the day in year is 29th of august. it accepts the date
* object or array [year, month, day].
*
* @param {*} dateobj
* Date object or array [year, month, day].
*/
function getDayOfTheYear(dateobj) {
const currentdate = Array.isArray(dateobj)
? new Date(dateobj.join("-"))
: dateobj;
const timestmp = new Date().setFullYear(new Date().getFullYear(), 0, 1);
const yearFirstDay = Math.floor(timestmp / 86400000);
const today = Math.ceil(currentdate.getTime() / 86400000);
const dayOfYear = today - yearFirstDay;
return dayOfYear + 1;
}
/**
* Total saving in year.
*/
let totalpay = 0;
for (let i = 1; i <= 12; i++) {
const currentPay = amountToPayInMonth(i, "2020");
totalpay = totalpay + currentPay;
}
// const pay = amountToPayInMonth("01", "2020");
console.log(totalpay);
// console.log(getDayOfTheYear(month.lastDay));
console.log(amountToPay(365));
view raw savings_func.js hosted with ❤ by GitHub

Copyright © 2022 Mitesh Patel. Built with Gatsby