3965e542fc
部署到群晖 / deploy (push) Failing after 6m22s
- 新增 server/:Node + Express + SQLite + node-cron 实现登录、纪念日 CRUD 和定时订阅消息推送 - 新增 .gitea/workflows/deploy.yml:推送即触发群晖 Docker 部署,监听 15002 - utils/api.js:自动按 envVersion 切换本地/线上 BASE_URL - app.js 与 add-anniversary.js 移除 wx.cloud 调用,改走自建后端 - cloudfunctions/ 暂保留以便回滚 - 一并提交此前未入库的首页 / 设置页 / 日历 / 万年历等改造 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
137 lines
3.0 KiB
JavaScript
137 lines
3.0 KiB
JavaScript
/**
|
|
* 日期工具函数
|
|
*/
|
|
|
|
/**
|
|
* 格式化日期
|
|
* @param {Date} date - 日期对象
|
|
* @param {String} format - 格式字符串,默认 'YYYY-MM-DD'
|
|
* @returns {String} 格式化后的日期字符串
|
|
*/
|
|
function formatDate(date, format = 'YYYY-MM-DD') {
|
|
if (!date) return ''
|
|
|
|
const d = new Date(date)
|
|
const year = d.getFullYear()
|
|
const month = String(d.getMonth() + 1).padStart(2, '0')
|
|
const day = String(d.getDate()).padStart(2, '0')
|
|
const hour = String(d.getHours()).padStart(2, '0')
|
|
const minute = String(d.getMinutes()).padStart(2, '0')
|
|
const second = String(d.getSeconds()).padStart(2, '0')
|
|
|
|
return format
|
|
.replace('YYYY', year)
|
|
.replace('MM', month)
|
|
.replace('DD', day)
|
|
.replace('HH', hour)
|
|
.replace('mm', minute)
|
|
.replace('ss', second)
|
|
}
|
|
|
|
/**
|
|
* 计算距离今天还有多少天
|
|
* @param {Date} targetDate - 目标日期
|
|
* @returns {Number} 天数差
|
|
*/
|
|
function getDaysUntil(targetDate) {
|
|
const today = new Date()
|
|
today.setHours(0, 0, 0, 0)
|
|
|
|
const target = new Date(targetDate)
|
|
target.setHours(0, 0, 0, 0)
|
|
|
|
const diff = target.getTime() - today.getTime()
|
|
return Math.ceil(diff / (1000 * 60 * 60 * 24))
|
|
}
|
|
|
|
/**
|
|
* 判断是否是今天
|
|
*/
|
|
function isToday(date) {
|
|
return getDaysUntil(date) === 0
|
|
}
|
|
|
|
/**
|
|
* 判断是否已过期
|
|
*/
|
|
function isPast(date) {
|
|
return getDaysUntil(date) < 0
|
|
}
|
|
|
|
/**
|
|
* 判断是否即将到来
|
|
*/
|
|
function isUpcoming(date) {
|
|
const days = getDaysUntil(date)
|
|
return days > 0 && days <= 7
|
|
}
|
|
|
|
/**
|
|
* 获取星期几
|
|
*/
|
|
function getWeekDay(date) {
|
|
const weekdays = ['日', '一', '二', '三', '四', '五', '六']
|
|
const d = new Date(date)
|
|
return weekdays[d.getDay()]
|
|
}
|
|
|
|
/**
|
|
* 判断是否在本月
|
|
*/
|
|
function isInThisMonth(date) {
|
|
const today = new Date()
|
|
const target = new Date(date)
|
|
return today.getFullYear() === target.getFullYear() &&
|
|
today.getMonth() === target.getMonth()
|
|
}
|
|
|
|
/**
|
|
* 获取本月的开始日期和结束日期
|
|
*/
|
|
function getMonthRange(year, month) {
|
|
const start = new Date(year, month - 1, 1)
|
|
const end = new Date(year, month, 0)
|
|
return { start, end }
|
|
}
|
|
|
|
/**
|
|
* 获取日期对象(从YYYY-MM-DD字符串)
|
|
*/
|
|
function parseDate(dateString) {
|
|
if (!dateString) return null
|
|
const parts = dateString.split('-')
|
|
return new Date(parseInt(parts[0]), parseInt(parts[1]) - 1, parseInt(parts[2]))
|
|
}
|
|
|
|
/**
|
|
* 计算指定月日的下一次发生日期(今年或明年)
|
|
* @param {Number} month - 月份 (1-12)
|
|
* @param {Number} day - 日 (1-31)
|
|
* @returns {{ date: Date, daysUntil: Number }}
|
|
*/
|
|
function getNextOccurrence(month, day) {
|
|
const today = new Date()
|
|
const currentYear = today.getFullYear()
|
|
let date = new Date(currentYear, month - 1, day)
|
|
let daysUntil = getDaysUntil(date)
|
|
if (daysUntil < 0) {
|
|
date = new Date(currentYear + 1, month - 1, day)
|
|
daysUntil = getDaysUntil(date)
|
|
}
|
|
return { date, daysUntil }
|
|
}
|
|
|
|
module.exports = {
|
|
formatDate,
|
|
getDaysUntil,
|
|
getNextOccurrence,
|
|
isToday,
|
|
isPast,
|
|
isUpcoming,
|
|
getWeekDay,
|
|
isInThisMonth,
|
|
getMonthRange,
|
|
parseDate
|
|
}
|
|
|