fix: 添加GetCurrentTime和IsDaysAgo函数,优化时间处理逻辑

This commit is contained in:
wangzhongjie 2025-03-24 14:28:27 +08:00
parent 509af0f154
commit 34756928dd
2 changed files with 20 additions and 10 deletions

View File

@ -1,5 +1,4 @@
import systemTime from '@ohos.systemDateTime';
import { timeSynchronization } from '../../api';
export function isSevenDaysAgo(date, days = 2) {
const today = new Date(); // 当前日期
@ -62,15 +61,15 @@ export function dateFormat(t) {
return year + "-" + fill(month) + "-" + fill(day) + " " + fill(hours) + ":" + fill(minutes) + ":" + fill(seconds);
}
export function dateVersionFormat(t) {
let year = t.getFullYear()
let month = t.getMonth() + 1
let day = t.getDate()
let hours = t.getHours()
let minutes = t.getMinutes()
let seconds = t.getSeconds()
return year + "." + fill(month) + "." + fill(day) + "." + fill(hours);
}
// export function dateVersionFormat(t) {
// let year = t.getFullYear()
// let month = t.getMonth() + 1
// let day = t.getDate()
// let hours = t.getHours()
// let minutes = t.getMinutes()
// let seconds = t.getSeconds()
// return year + "." + fill(month) + "." + fill(day) + "." + fill(hours);
// }
//同步时时间

View File

@ -4,10 +4,21 @@ enum timeType {
fulltime = 1
}
// 获取当前时间
export function GetCurrentTime(type?: timeType): string {
if (type === 1) {
return dayTs().format("YYYYMMDDHHmmss")
} else {
return dayTs().format("YYYY-MM-DD HH:mm:ss")
}
}
//是否是多少天前
export function IsDaysAgo(date: string | number | Date, days: number = 2): boolean {
const today = dayTs(); // 当前日期
const target = dayTs(date).format("YYYY-MM-DD HH:mm:ss.SSS"); // 需要判断的日期
// 计算两个日期之间的差异天数
const diffDays = today.diff(target, 'day');
// 如果差异天数大于等于指定天数,则返回 true
return diffDays >= days;
}