存钱计算小工具,为了自己更富有,努力存钱哦

// 周存法计算器 银行利率是波动此处只做参考
/**
 *  每周存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 可以好好学习一下,具体的百度

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值