用每天的差异计算日期和时间

Calculate date & time with each day difference

所以,我有一张这样的光盘

let events = {
      "KOTH Airship": ["EVERY 19:00"],
      "KOTH Castle": ["EVERY 20:00"],
      Totem: ["EVERY 17:00", "EVERY 23:00"],
      Jump: ["Monday 18:00"],
      "Laby PvP": ["Tuesday 18:00"],
      "MegaKOTH Nether": ["Wednesday 18:00"],
      AirDrop: ["Thusday 18:00"],
      "Rain of Items": ["Friday 18:00"],
      "MegaKOTH Pyramid": ["Saturday 18:00"],
      "MegaKOTH End": ["Sunday 18:00"],
    };

我想计算从今天到第二天的剩余时间(用日期名称或第二天,即每一天),我应该怎么做?

我猜“每个”是指第二天。

你的数据是这样的:

[...days|every] [hour, minute]

我的代码是:

class EventDate extends Date {
    calculate(data) {
        let result = { day: 0, hour: 0, minute: 0, second: 0 };
        let now = Date.now();
        this.setByParsing(data);

        let diff = this.getTime() - now;
        result.time = diff;
        result.second = diff / 1000;
        result.minute = result.second / 60;
        result.hour = result.minute / 60;
        result.day = result.hour / 24;
        result.second = result.second >= 0 ? Math.floor(result.second % 60) : 0;
        result.minute = result.minute >= 0 ? Math.floor(result.minute % 60) : 0;
        result.hour = result.hour >= 0 ? Math.floor(result.hour % 24) : 0;
        result.day = result.day >= 0 ? Math.floor(result.day) : 0;
        return result;
    }

    getByParsing(data) {
        let result = {};
        let dayNames = this.getDayNames();
        let parts = data.split(' ');
        let currentDate = new Date();

        if (parts) {
            let pDayName = parts[0].toLowerCase();
            let dayInd = dayNames.indexOf(pDayName);
            let day = this.getDay();
            let date = this.getDate();
            let parsedTime = this.parseTime(data);

            if (dayInd < 0) {
                date++;
                day++;
                if (day > 6) day = 0;
            }
            else {
                if (currentDate.getDay() === dayInd && this.isTimeEarlier(parsedTime, { hour: currentDate.getHours(), minute: currentDate.getMinutes(), second: currentDate.getSeconds() })) date += 7;
                else date += Math.abs(dayInd - day);
                day = dayInd;
            }
        
            result.day = day;
            result.date = date; 
            result = { ...result, ...parsedTime };
        }
    
        return result;
    }

    setByParsing(data) {
        let parsing = this.getByParsing(data);
        if ('date' in parsing) this.setDate(parsing.date);
        if ('hour' in parsing) this.setHours(parsing.hour);
        if ('minute' in parsing) this.setMinutes(parsing.minute);
        if ('second' in parsing) this.setSeconds(parsing.second);
        return this;
    }

    parseTime(data) {
        let result = { hour: 0, minute: 0, second: 0 };
        let parts = data.split(' ');

        if (parts && '1' in parts) {
            let timeParts = parts[1].split(':');
            result.hour = timeParts[0] || 0;
            result.minute = timeParts[1] || 0;
            result.second = timeParts[2] || 0;
        }
    
        return result;
    }

    isTimeEarlier(time1, time2) {
        if (time1.hour < time2.hour) return true;
        else if (time1.minute < time2.minute) return true;
        else if (time1.second < time2.second) return true;
    }

    getDayNames() {
        let date = new Date();
        date.setDate(date.getDate() - date.getDay());

        return [...Array(7)].map((val, ind) => {
            let result = date.toLocaleString('en-us', { weekday: 'long' });
            date.setDate(date.getDate() + 1);
            return result.toLowerCase();
        });
    }
}

let times = {};

Object.keys(events).forEach(name => {
    times[name] = [];
    
    events[name].forEach(data => {
        let dataDate = new EventDate();
        times[name].push(dataDate.calculate(data));
    });
});

console.log(times);