首页 > 其他分享 >js获取未来n天的时间并返回是否是今天,日期yyyy-mm-dd和星期

js获取未来n天的时间并返回是否是今天,日期yyyy-mm-dd和星期

时间:2024-08-05 10:44:00浏览次数:15  
标签:02 week false mm dd js 2024 isToday date

js获取未来n天的时间.md

2024.02.02(今天)如果传入5则获取未来5天的时间数组
[
	{date: "2024-02-02,"isToday: true,week: "星期五"},
	{date: "2024-02-03,"isToday: false,week: "星期六"},
	{date: "2024-02-04,"isToday: false,week: "星期日"},
	{date: "2024-02-05,"isToday: false,week: "星期一"},
	{date: "2024-02-06,"isToday: false,week: "星期二"},
]
initPredictionTime(num) {
    // 获取今天的日期
    let today = new Date();
    // 定义星期的数组
    let weekdays = [
        "星期天",
        "星期一",
        "星期二",
        "星期三",
        "星期四",
        "星期五",
        "星期六",
    ];
    // 获取今天是一周中的第几天(星期几)
    this.predictionWeek = weekdays[today.getDay()];
    // 今天及接下来一周的日期和星期几
    const result = [];
    for (let i = 1; i <= num; i++) {
        const dateString =
              today.getFullYear() +
              "-" +
              (today.getMonth() + 1).toString().padStart(2, "0") +
              "-" +
              today.getDate().toString().padStart(2, "0");
        const week = weekdays[today.getDay()];
        result.push({ week, date: dateString });
        today.setDate(today.getDate() + 1);
    }
    result.find(item => item.week === this.predictionWeek).isToday = true;
    return result;
},

标签:02,week,false,mm,dd,js,2024,isToday,date
From: https://www.cnblogs.com/ProgrammerMao-001/p/18342767

相关文章