Skip to content

Commit

Permalink
fix: 🐛时间比较方法问题
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyjone committed May 12, 2023
1 parent 8e559a5 commit dcedfe7
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/models/param/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { day, formatDate } from '@/utils/date';
enum DateEnum {
'year',
'month',
'week',
'day',
'hour',
'minute',
'second'
'second',
'millisecond'
}

/**
Expand Down Expand Up @@ -71,17 +73,17 @@ export class XDate {
* @param precision 精度,可以通过不同单位来调整判断精度
*/
isSame(date: XDate, precision: DateUnit) {
if (precision === 'week') {
return day(this.date).week() === day(date.date).week();
}
const t = this.date.toLocaleString().split(/\s|\/|:/);
t.splice(2, 0, day(this.date).week().toString());
t.push(this.date.getMilliseconds().toString());

if (precision === 'millisecond') {
return this.date.getMilliseconds() === date.date.getMilliseconds();
}
const d = date.date.toLocaleString().split(/\s|\/|:/);
d.splice(2, 0, day(date.date).week().toString());
d.push(date.date.getMilliseconds().toString());

return (
this.date.toLocaleString().split(/\s|\/|:/)[DateEnum[precision]] ===
date.date.toLocaleString().split(/\s|\/|:/)[DateEnum[precision]]
t.slice(0, DateEnum[precision] + 1).join('') ===
d.slice(0, DateEnum[precision] + 1).join('')
);
}

Expand All @@ -96,12 +98,11 @@ export class XDate {
* 通过不同单位获取当前时间的不同精度值
*/
getBy(unit: DateUnit) {
if (unit === 'week') return day(this.date).week();
if (unit === 'millisecond') return this.date.getMilliseconds();
const t = this.date.toLocaleString().split(/\s|\/|:/);
t.splice(2, 0, day(this.date).week().toString());
t.push(this.date.getMilliseconds().toString());

return parseInt(
this.date.toLocaleString().split(/\s|\/|:/)[DateEnum[unit]]
);
return parseInt(t[DateEnum[unit]]);
}

/**
Expand Down

0 comments on commit dcedfe7

Please sign in to comment.