You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
5.4 KiB
JavaScript

4 weeks ago
class DayConverter {
constructor(data) {
this.nameToValueMap = {};
this.valueToNameMap = {};
// 初始化映射关系
data.forEach(item => {
this.nameToValueMap[item.dayName] = item.dayValue;
this.valueToNameMap[item.dayValue] = item.dayName;
});
}
// 根据dayName获取dayValue
getValueByName(dayName) {
return this.nameToValueMap[dayName];
}
// 根据dayValue获取dayName
getNameByValue(dayValue) {
return this.valueToNameMap[dayValue];
}
// 根据dayName获取对应日期
getDateByDayName(dayName) {
const today = new Date();
const dayValue = this.getValueByName(dayName);
if (dayValue === undefined) {
throw new Error(`Invalid dayName: ${dayName}`);
}
const targetDate = new Date(today);
targetDate.setDate(today.getDate() - dayValue);
// 格式化为YYYY-MM-DD
const year = targetDate.getFullYear();
const month = String(targetDate.getMonth() + 1).padStart(2, '0');
const day = String(targetDate.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
// return targetDate;
}
/**
* -1 获取今天昨天前天的数据
* 0 获取今天
* 1 获取昨天
* 2 获取前天
* @param {时间} dateTime
*/
getDayParam(dateTime,point){
console.log(`获取参数,dateTime: ${dateTime},point:${point}`);
// 正确解析日期(处理时区问题)
const parseDate = (dateStr) => {
const [year, month, day] = dateStr.split('-').slice(0, 3).map(Number);
return new Date(year, month - 1, day); // 月份要减1
};
const cachedDateObj = parseDate(dateTime);
if (isNaN(cachedDateObj.getTime())) {
console.error('无效的日期');
return -1;
}
// 获取今天、昨天、前天的日期(统一时区处理)
const now = new Date();
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
const yesterday = new Date(today);
yesterday.setDate(yesterday.getDate() - 1);
const dayBeforeYesterday = new Date(today);
dayBeforeYesterday.setDate(dayBeforeYesterday.getDate() - 2);
// 比较日期
if (this.isSameDay(cachedDateObj, today)) {
return [{"day": 0, "package": point}]; // 今天
} else if (this.isSameDay(cachedDateObj, yesterday)) {
return [{"day": 0, "package": 0}, {"day": 1, "package": point}]; // 昨天
}
// else if (this.isSameDay(cachedDateObj, dayBeforeYesterday)) {
// return [{"day": 0, "package": 0}, {"day": 1, "package": 0}, {"day": 2, "package": point}]; //
// }
else {
return [{"day": 0, "package": 0}, {"day": 1, "package": 0}, {"day": 2, "package": point}]; // 前天或更早的日期
}
}
/**
* 判断两个Date对象是否是同一天
* @param {Date} date1
* @param {Date} date2
* @returns {boolean}
*/
isSameDay(date1, date2) {
return date1.getFullYear() === date2.getFullYear() &&
date1.getMonth() === date2.getMonth() &&
date1.getDate() === date2.getDate();
}
/**
* 13 位时间戳例子1751295900000
* @param {string} fallAsleepTime 例子06-30-23-05
*/
formatFullDateTime(fallAsleepTime){
if (!fallAsleepTime || typeof fallAsleepTime !== 'string') {
throw new Error('Invalid fallAsleepTime format. Expected "MM-dd-HH-mm"');
}
const currentYear = new Date().getFullYear();
// 拼接成 "2025-06-30-23-05"
const fullDateTimeString = `${currentYear}-${fallAsleepTime}`;
// 转换为标准格式 "2025-06-30T23:05"
const isoFormat = fullDateTimeString.replace(/-(\d{2})-(\d{2})$/, 'T$1:$2');
// 创建 Date 对象并获取时间戳
const timestamp = new Date(isoFormat).getTime();
console.log("完整日期时间字符串:", fullDateTimeString);
console.log("ISO 格式:", isoFormat);
console.log("时间戳:", timestamp);
return timestamp;
}
/**
* 计算时间戳对应的当天分钟数0-1439
* @param {number} timestamp - 13位毫秒级时间戳
* @returns {number} 分钟数
*/
getMinuteOfDay(timestamp) {
const date = new Date(timestamp);
return date.getHours() * 60 + date.getMinutes();
}
/**
* 睡眠片段日期格式化
* @param {*} timestamp
*/
formatDate(timestamp) {
const date = new Date(timestamp);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${year}${month}${day}`;
}
/**
* 获取1天的第N分钟
* @param {Number} timeString 例子06-30-23-05
*/
getMinutesFromTime(timeString) {
const dateParts = timeString.split('-');
if (dateParts.length !== 4) {
throw new Error("时间格式必须是 MM-DD-HH-mm");
}
const hour = parseInt(dateParts[2], 10); // 明确转换为十进制整数
const minute = parseInt(dateParts[3], 10);
if (isNaN(hour) || isNaN(minute)) {
throw new Error("小时和分钟必须是数字");
}
if (hour < 0 || hour >= 24 || minute < 0 || minute >= 60) {
throw new Error("小时需在 0-23 之间,分钟需在 0-59 之间");
}
return hour * 60 + minute;
}
}
// 使用示例
const data = [
{"dayName":"today","dayValue":0},
{"dayName":"yesterday","dayValue":1},
{"dayName":"dayBeforeYesterday","dayValue":2}
];
const dayConverter = new DayConverter(data);
export default dayConverter;