// 周存法计算器 银行利率是波动此处只做参考
/**
* 每周存500 第二周1000第三周1500 setpNum=500
* 满countNum=10000元,开始存定期, 活期利息忽略不计算。
*
* countDeposit 方法,计算多少年以后的日期
* 默认开始时间和结束时间
* let startDate = "2019-3-30"; let endDate = "2034-3-30";
* 计算方法,复利计算,
*
*/
let longTh = 52; //一年按照52周计算
let setpNum = 500;
let startNum = 0;
let countNum = 10000;
let count = 0;
let listCount = [];
let dateList = []; // 开始日期和结束日期。
const weekTime = 7 * 24 * 60 * 60 * 1000;
function yearDeposit(weekCount, startDate) {
let weekLength = weekCount || longTh;
for (let i = 0; i < weekLength; i++) {
let weekNum = i + 1;
startNum = startNum + setpNum;
listCount.push(startNum);
const total = listCount.reduce((a, b) => {
return a + b;
});
console.log(`第 ${weekNum} 周:存入${startNum}`);
if (countNum <= total) {
startNum = 0;
listCount = [];
count = count + countNum;
if (startDate) {
let endDate = dateSubWeek(startDate, weekNum);
dateList.push(endDate);
}
console.log(
`-------第${weekNum}周:存入1W定期3年----,余下存款${total -
countNum}奖励自己消费一下`
);
}
}
console.log("最终定期存款本金" + count);
}
// 日期计算方法
function dateSubWeek(startDate, n) {
let startDateTime = new Date(startDate).getTime();
let endDateTime = startDateTime + n * weekTime;
return dateFormat(new Date(endDateTime))
.split("/")
.join("-");
}
//复利时间计算
function compoundingDate(startDate, yearLength, endDate) {
let startDateTime = new Date(startDate).getTime();
let endDateTime = new Date(endDate).getTime();
let yearTime = yearLength * 365 * 24 * 60 * 60 * 1000;
let list = [];
while (startDateTime < endDateTime) {
list.push(
dateFormat(new Date(startDateTime))
.split("/")
.join("-")
);
startDateTime = startDateTime + yearTime;
}
return list;
}
// 日期格式化
function dateFormat(dateCN) {
const formatterCN = new Intl.DateTimeFormat("zh-CN", {
timeZone: "Asia/Shanghai",
year: "numeric",
month: "2-digit",
day: "2-digit",
// hour: "2-digit",
// minute: "2-digit",
// second: "2-digit"
});
return formatterCN.format(dateCN); // 2023/02/19
}
// 存钱计算器 开始日期 结束日期 周期yearLength3 年化利率
function countDeposit() {
let startDate = "2019-3-30";
let endDate = "2034-3-30";
// 年处理
let yearLength = 3; // 3你那定期为例
let annualRate = 0.03; // 每年利率
let weekNum =
(new Date(endDate).getTime() - new Date(startDate).getTime()) /
weekTime;
console.log(weekNum);
yearDeposit(weekNum, startDate);
let newDateList = dateList.map((item, index) => {
console.log(`-------${item}-----`, index + 1);
return compoundingDate(item, yearLength, endDate);
});
let compoundingCount = 0;
//每一笔复利计算和到期时间
newDateList
.filter(item => item.length > 0)
.forEach((item, index) => {
let practicalNum = 0;
item.forEach((childrenItem, childrenIndex) => {
practicalNum =
countNum * Math.pow(1 + annualRate * 3, childrenIndex);
console.log(
`第${index + 1}笔的 第${childrenIndex +
1}期时间-------${childrenItem}--------存入${practicalNum}`
);
});
console.log(
`第${index + 1}笔在---${
item[item.length - 1]
}---最终收益${practicalNum}`
);
compoundingCount += practicalNum;
});
console.log(
`最终日期:-----${endDate}------总收益:------${compoundingCount}-----`
);
}
countDeposit();
只是为了激励自己存钱,关于里面时间格式化对象new Intl.DateTimeFormat 可以好好学习一下,具体的百度