1090 字
5 分钟
给女朋友写每日推送
const gdkey = 'xxxx' //高德地图key const city = 'xxxx' //城市 function getTimeDifferenceToNextFifteenth() { const today = new Date(); const currentMonth = today.getMonth(); const currentYear = today.getFullYear();
// 下个月的15号 const fifteenthNextMonth = new Date(currentYear, currentMonth + 1, 15);
// 本月的15号 const fifteenthThisMonth = new Date(currentYear, currentMonth, 15);
// 如果今天是15号或之后,使用下个月的15号 const nextFifteenth = today >= fifteenthThisMonth ? fifteenthNextMonth : fifteenthThisMonth;
// 计算时间差(毫秒) const timeDifference = nextFifteenth - today;
// 转换为天数 const daysDifference = Math.ceil(timeDifference / (1000 * 60 * 60 * 24));
return daysDifference
} function calculateDaysDifference() { const targetDate = "2024-3-22"; // 指定的目标日期 const today = new Date(); const target = new Date(targetDate);
// 计算时间差(毫秒) const timeDifference = today - target;
// 转换为天数 const daysDifference = Math.ceil(timeDifference / (1000 * 60 * 60 * 24));
return daysDifference - 1; }
function getDayOfWeek() { const date = new Date(); const dayOfWeek = date.getDay(); // 返回值为0(周日)到6(周六) // 将返回值转换为1(周一)到7(周日) return dayOfWeek === 0 ? 7 : dayOfWeek; }
function getFormattedTime() { const date = new Date(); const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始 const day = String(date.getDate()).padStart(2, '0'); return `${year}年${month}月${day}日`; } // http://timor.tech/api/holiday/year const holidaysData = { "01-01": { "holiday": true, "name": "元旦", "wage": 3, "date": "2024-01-01", "rest": 1 }, "02-10": { "holiday": true, "name": "初一", "wage": 3, "date": "2024-02-10", "rest": 4 }, "02-11": { "holiday": true, "name": "初二", "wage": 3, "date": "2024-02-11", "rest": 1 }, "02-12": { "holiday": true, "name": "初三", "wage": 3, "date": "2024-02-12", "rest": 1 }, "02-13": { "holiday": true, "name": "初四", "wage": 2, "date": "2024-02-13", "rest": 1 }, "02-14": { "holiday": true, "name": "初五", "wage": 2, "date": "2024-02-14", "rest": 1 }, "02-15": { "holiday": true, "name": "初六", "wage": 2, "date": "2024-02-15", "rest": 1 }, "02-16": { "holiday": true, "name": "初七", "wage": 2, "date": "2024-02-16", "rest": 1 }, "02-17": { "holiday": true, "name": "初八", "wage": 2, "date": "2024-02-17", "rest": 1 }, "04-04": { "holiday": true, "name": "清明节", "wage": 3, "date": "2024-04-04", "rest": 1 }, "04-05": { "holiday": true, "name": "清明节", "wage": 2, "date": "2024-04-05", "rest": 1 }, "04-06": { "holiday": true, "name": "清明节", "wage": 2, "date": "2024-04-06", "rest": 1 }, "05-01": { "holiday": true, "name": "劳动节", "wage": 3, "date": "2024-05-01", "rest": 17 }, "05-02": { "holiday": true, "name": "劳动节", "wage": 2, "date": "2024-05-02", "rest": 1 }, "05-03": { "holiday": true, "name": "劳动节", "wage": 3, "date": "2024-05-03", "rest": 1 }, "05-04": { "holiday": true, "name": "劳动节", "wage": 3, "date": "2024-05-04", "rest": 1 }, "05-05": { "holiday": true, "name": "劳动节", "wage": 3, "date": "2024-05-05", "rest": 1 }, "06-08": { "holiday": true, "name": "端午节", "wage": 2, "date": "2024-06-08", "rest": 30 }, "06-09": { "holiday": true, "name": "端午节", "wage": 2, "date": "2024-06-09", "rest": 1 }, "06-10": { "holiday": true, "name": "端午节", "wage": 3, "date": "2024-06-10", "rest": 1 }, "09-15": { "holiday": true, "name": "中秋节", "wage": 2, "date": "2024-09-15", "rest": 24 }, "09-16": { "holiday": true, "name": "中秋节", "wage": 2, "date": "2024-09-16", "rest": 1 }, "09-17": { "holiday": true, "name": "中秋节", "wage": 3, "date": "2024-09-17", "rest": 1 }, "10-01": { "holiday": true, "name": "国庆节", "wage": 3, "date": "2024-10-01", "rest": 13 }, "10-02": { "holiday": true, "name": "国庆节", "wage": 3, "date": "2024-10-02", "rest": 1 }, "10-03": { "holiday": true, "name": "国庆节", "wage": 3, "date": "2024-10-03", "rest": 1 }, "10-04": { "holiday": true, "name": "国庆节", "wage": 2, "date": "2024-10-04", "rest": 1 }, "10-05": { "holiday": true, "name": "国庆节", "wage": 2, "date": "2024-10-05", "rest": 1 }, "10-06": { "holiday": true, "name": "国庆节", "wage": 2, "date": "2024-10-06", "rest": 1 }, "10-07": { "holiday": true, "name": "国庆节", "wage": 2, "date": "2024-10-07", "rest": 1 } }; function getNextHoliday() { const today = new Date(); console.log(today) let nextHoliday = null; let daysDifference = null;
for (const key in holidaysData) { const holiday = holidaysData[key]; const holidayDate = new Date(holiday.date);
if (holidayDate >= today) { console.log(holidayDate) if (nextHoliday === null || holidayDate < nextHoliday.date) { nextHoliday = holiday; daysDifference = Math.ceil((holidayDate - today) / (1000 * 60 * 60 * 24)); } if (holidayDate === today) { return `今天是【${holiday.name}】`; } } }
if (nextHoliday) { return `距离最近的节假日是【${nextHoliday.name}】还有${daysDifference}天`; } else { return '没有更多节假日'; } }
function checkIfTodayIsHoliday() { const today = new Date(); const todayString = today.toISOString().split('T')[0];
for (const key in holidaysData) { const holiday = holidaysData[key]; if (holiday.date === todayString && holiday.holiday) { return `今天就是【${holiday.name}】`; } }
return getNextHoliday(); } let tianqi try { allday = await request({ url: $`https://restapi.amap.com/v3/weather/weatherInfo?key=${gdkey}&city=${city}&extensions=all`,json: true }); today = allday.body.forecasts[0].casts[0] console.log(today) tianqi = `白天${today['dayweather']},夜晚${today['nightweather']},最高温度${today['daytemp']},最低温度${today['nighttemp']},${today['daywind']}风${today['daypower']}级` } catch (error) { console.log(error) tianqi = '' }
let yiyan try { const options = { method: 'POST', url: 'xxxx',//每日一言接口 body: 'xxxx', json: true }; yiyan = await request(options); yiyan = yiyan.body.data.result.answer } catch (error) { yiyan = '今天也是元气满满的一天哦~' } // 如果getDayOfWeek的值为6或7,说明今天是周末 const zhoumo = getDayOfWeek() === 6 || getDayOfWeek() === 7 ? '今天是周末哦~' : `距离【周末】还有${6 - getDayOfWeek()}天`; const fagongzi = getTimeDifferenceToNextFifteenth() === 0 ? '今天发工资啦!' : `距离【发工资】还有${getTimeDifferenceToNextFifteenth()}天`; const tsmsg = `[CQ:face,id=319]和宝宝相恋【${calculateDaysDifference()}】天[CQ:face,id=319]\n[CQ:face,id=351]今天是${getFormattedTime()}[CQ:face,id=351]\n[CQ:face,id=293]是本周的第${getDayOfWeek()}天[CQ:face,id=293]\n[CQ:face,id=332]${zhoumo}[CQ:face,id=332]\n[CQ:face,id=332]${fagongzi}[CQ:face,id=332]\n[CQ:face,id=332]${checkIfTodayIsHoliday()}[CQ:face,id=332]\n[CQ:face,id=333]${tianqi}[CQ:face,id=333]\n【[CQ:face,id=63]${yiyan}[CQ:face,id=63]】`效果展示:
