wt-date-utils
v1.0.0
Published
Word Tutor 日期处理工具库,支持北京时间2AM日期分界规则
Downloads
84
Maintainers
Readme
wt-date-utils
Word Tutor 日期处理工具库,支持北京时间 2AM 日期分界规则。
安装
npm install wt-date-utils核心规则
- 时区:北京时间(UTC+8)
- 日期分界线:北京时间凌晨 02:00(对应 UTC 18:00)
- 分界规则:
- 北京时间 00:00-01:59:59 算作前一天
- 北京时间 02:00-23:59:59 算作当天
使用示例
const DateUtils = require('wt-date-utils');
// 获取学习日期
const learnDate = DateUtils.getLearnDate('2025-01-05T17:30:00.000Z');
// 北京时间 01:30 → 返回 '2025-01-05'(算作前一天)
const learnDate2 = DateUtils.getLearnDate('2025-01-05T18:30:00.000Z');
// 北京时间 02:30 → 返回 '2025-01-06'(算作当天)
// 计算复习日期
const reviewDates = DateUtils.calculateReviewDates('2025-01-05', [1, 2, 4, 7]);
// 返回 ['2025-01-06', '2025-01-07', '2025-01-09', '2025-01-12']
// 获取学习日期的 UTC 时间范围
const range = DateUtils.getLearnDateUTCRange('2025-01-05');
// range.start = UTC 2025-01-04 18:00:00.000
// range.end = UTC 2025-01-05 17:59:59.999
// 检查时间是否属于某个学习日期
const isInRange = DateUtils.isInLearnDateRange('2025-01-05T10:00:00.000Z', '2025-01-05');
// 返回 trueAPI 文档
常量
BEIJING_OFFSET_MS- 北京时间偏移量(毫秒),值为8 * 60 * 60 * 1000BOUNDARY_HOUR- 日期分界小时,值为2
函数
toBeijingTime(utcTime)
将 UTC 时间转换为北京时间。
- 参数:
utcTime- Date 对象、ISO 字符串或时间戳 - 返回: Date 对象(北京时间),无效输入返回 Invalid Date
getNowBeijing()
获取当前北京时间。
- 返回: Date 对象(当前北京时间)
getLearnDate(utcTime)
根据 2AM 分界规则,从时间戳计算学习日期。
- 参数:
utcTime- UTC 时间戳 - 返回: 学习日期字符串
YYYY-MM-DD,无效输入返回空字符串
isInLearnDateRange(utcTime, learnDate)
检查时间戳是否属于指定学习日期。
- 参数:
utcTime- UTC 时间戳learnDate- 学习日期YYYY-MM-DD
- 返回: boolean
getLearnDateUTCRange(learnDate)
获取学习日期对应的 UTC 时间范围。
- 参数:
learnDate- 学习日期YYYY-MM-DD - 返回:
{ start: Date, end: Date }UTC 时间范围
calculateReviewDates(learnDate, reviewPlan)
计算复习日期数组。
- 参数:
learnDate- 学习日期YYYY-MM-DDreviewPlan- 复习计划天数数组,如[1, 2, 4, 7]
- 返回: 复习日期数组,无效输入返回空数组
formatDateOnly(date)
格式化日期为 YYYY-MM-DD 字符串。
- 参数:
date- Date 对象 - 返回: 格式化的日期字符串,无效输入返回空字符串
parseDateString(dateStr)
解析 YYYY-MM-DD 字符串为 Date 对象(UTC 0点)。
- 参数:
dateStr- 日期字符串 - 返回: Date 对象,无效输入返回 Invalid Date
calculateReviewIndex(learnTime, reviewDate, reviewPlan)
计算复习索引。
- 参数:
learnTime- 学习时间reviewDate- 复习日期YYYY-MM-DDreviewPlan- 复习计划数组
- 返回: 复习索引,未找到返回 -1
时间范围说明
UTC 时间轴:
|-------|-------|-------|-------|-------|-------|
12:00 14:00 16:00 18:00 20:00 22:00 00:00
北京时间轴 (UTC+8):
|-------|-------|-------|-------|-------|-------|
20:00 22:00 00:00 02:00 04:00 06:00 08:00
↑ ↑
前一天 当天开始
结束 (分界线)License
MIT
