Skip to content

解决两个精度丢失问题 #16

@1124863805

Description

@1124863805
subtractMinute(solar: Solar): number {
    let days = this.subtract(solar);
    const cm = this._hour * 60 + this._minute;
    const sm = solar.getHour() * 60 + solar.getMinute();
    let m = cm - sm;
    if (m < 0) {
        m += 1440;
        days--;
    }
    m += days * 1440;
    return m;
}

修改之后

subtractMinute(solar: Solar): number {
    const thisDateTime = new Date(this._year, this._month - 1, this._day, this._hour, this._minute, this._second);
    const solarDateTime = new Date(solar.getYear(), solar.getMonth() - 1, solar.getDay(), solar.getHour(), solar.getMinute(), solar.getSecond());
    
    const diffMs = thisDateTime.getTime() - solarDateTime.getTime();
    const diffMinutes = Math.floor(diffMs / (1000 * 60));
    
    return diffMinutes;
}

Solar 文件

Yun.ts

if (2 === sect) {
let minutes = end.subtractMinute(start);
year = Math.floor(minutes / 4320);
minutes -= year * 4320;
month = Math.floor(minutes / 360);
minutes -= month * 360;
day = Math.floor(minutes / 12);
minutes -= day * 12;
hour = minutes * 2;
}

if (2 === sect) {
// 使用精确的分钟数计算,保持精度直到最后一步
const thisDateTime = new Date(end.getYear(), end.getMonth() - 1, end.getDay(), end.getHour(), end.getMinute(), end.getSecond());
const startDateTime = new Date(start.getYear(), start.getMonth() - 1, start.getDay(), start.getHour(), start.getMinute(), start.getSecond());
const diffMs = thisDateTime.getTime() - startDateTime.getTime();
let minutes = diffMs / (1000 * 60); // 保持精确的分钟数,不使用Math.floor

        year = Math.floor(minutes / 4320);
        minutes -= year * 4320;
        month = Math.floor(minutes / 360);
        minutes -= month * 360;
        day = Math.floor(minutes / 12);
        minutes -= day * 12;
        hour = Math.floor(minutes * 2); // 在最后一步才使用Math.floor
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions