【问题标题】:Calculate date & time with each day difference计算日期和时间与每天的差异
【发布时间】:2021-10-10 09:44:43
【问题描述】:

所以,我有这样的唱片

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"],
    };

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

【问题讨论】:

    标签: javascript typescript date time days


    【解决方案1】:

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

    你的数据是这样的:

    [...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);
    

    【讨论】:

    • 它按预期工作,但如果日期已经消失,我会得到否定的日期!
    • 我明白了。你的意思是如果数据是 [今天] [早期],它会给出负数。我正在更新答案。
    • 另外,如果您检查时间是否到了,请使用“[time].time > 0”。
    • 我忘了一行。再次编辑。
    • 现在日期都是正数,但它们变成了 0d 0h 0m 和 0s。我希望它针对即将到来的星期日(以防今天)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    • 2010-12-09
    • 2016-06-22
    • 2016-04-03
    • 2016-08-14
    • 2016-04-26
    相关资源
    最近更新 更多