JavaScript减去时间差异

时间:2016-12-27 23:55:37

标签: javascript

我刚刚制作了以下代码并且遇到了一些问题。该项目的整个想法是能够确定您在学校期间或在一段时间开始之前离开的时间。我首先将用户时间存储为变量,并使用子字符串隔离当前的小时和分钟。这是代码。

//Get current date.
var now = new Date();
//Make the date only hours and minutes But the issue is there is a semicolon (:) in between them.
var myTime = now.toString().substr(16,5)

问题在于我留下了一个分号,可以阻止整个工作。

在我将小时和分钟隔离并存储为变量之后,我需要判断用户的时间是否在学校日期之内。我通过制作一个我不确定已成功完成的条件if语句来做到这一点。

if (myTime > startday || myTime < endday) {

这里我只希望语句能够工作,如果用户当前时间(也称为myTime)比开始日更长,而且比结束日少。这是因为开始日存储为我上学日的开始时间,而结束日则存储为我上学日的结束时间。如果用户当前时间剂量满足此要求,则转到其他声明,即学校尚未开始。

如果您想查看并提出任何建议,请参阅其余代码。

//Get current date.
var now = new Date();
//Make the date only hours and minutes But the issue is there is a semicolen (:) in between them.
var myTime = now.toString().substr(16,5)
//For Testing Purposes.
console.log(myTime);

//All the periods throught my school day.
var startday = 0745;
var period1end = 0834;
var period1transition = 0838;
var period2end = 0930;
var period2transition = 0934;
var period3end = 1023;
var period3transition = 1027;
var period4end = 1116;
var period4transition = 1120;
var period5end = 1238;
var period5transition = 1242;
var period6end = 1331;
var period6transition = 1335;
var period7end = 1425;
var endday = 1425;

//The big boy, I will tell you my thought proccess as I go
function myFunction() {
//Here I ONLY want the statement to work if the users current time, also known as myTime is GREATER than startday and less than endday. This is because startday is stored as the start of my school day and endday is the end of the day. If the users current time dosent meet this requirement it then goes to the else statement which says school hasnt started yet.
    if (myTime > startday || myTime < endday) {
        console.log("Test");
//The purpose of this statement is the following. If the users time is LESS than the time period 1 ends it will then subtract period1's time from the users current time.
    } else if (myTime < period1end) {
        var timeleft = (period1end-myTime);
        console.log("There is" + timeleft + "mintues left in the period." );
//The purpose of this statement is if the users time is in between a period, also known as period1transition, it says the amount of time until the next period starts.
    } else if (myTime < period1transition) {
        var timeleft = (period1transition-myTime);
        console.log("There is" + timeleft + "mintues left until the next period starts." );
    } else if (myTime < period2end) {
        var timeleft = (period2end-myTime);
        console.log("There is" + timeleft + "mintues left in the period." );
    } else if (myTime < period2transition) {
        var timeleft = (period2transition-myTime);
        console.log("There is" + timeleft + "mintues left until the next period starts." );
    } else if (myTime < period3end) {
        var timeleft = (period3end-myTime);
        console.log("There is" + timeleft + "mintues left in the period." );
    } else if (myTime < period3transition) {
        var timeleft = (period3transition-myTime);
        console.log("There is" + timeleft + "mintues left until the next period starts." );
    } else if (myTime < period4end) {
        var timeleft = (period4end-myTime);
        console.log("There is" + timeleft + "mintues left in the period." );
    } else if (myTime < period4transition) {
        var timeleft = (period4transition-myTime);
        console.log("There is" + timeleft + "mintues left until the next period starts." );
    } else if (myTime < period5end) {
        var timeleft = (period5end-myTime);
        console.log("There is" + timeleft + "mintues left in the period." );
    } else if (myTime < period5transition) {
        var timeleft = (period5transition-myTime);
        console.log("There is" + timeleft + "mintues left until the next period starts." );
    } else if (myTime < period6end) {
        var timeleft = (period6end-myTime);
        console.log("There is" + timeleft + "mintues left in the period." );
    } else if (myTime < period6transition) {
        var timeleft = (period6transition-myTime);
        console.log("There is" + timeleft + "mintues left until the next period starts." );
    } else if (myTime < period7end) {
        var timeleft = (period7end-myTime);
        console.log("There is" + timeleft + "mintues left in the period." );
    } else if (myTime < period7transition) {
        var timeleft = (period7transition-myTime);
        console.log("There is" + timeleft + "mintues left until the next period starts." );
    } else {
//If the users time is not greater than the startday and less than the endday it simply says the following.
        console.log("School Has Not Started Yet");
    }
  //forget about this.
// document.getElementById("demo").innerHTML = myTime;
}

由于

扎克

2 个答案:

答案 0 :(得分:0)

您应该只使用分钟而不是小时 - 分钟组合,因为后者更难以计算。

此外,如果使用数组存储不同的句点,则代码的重复次数会减少。

以下是如何做到这一点:

&#13;
&#13;
//Get current date & midnight
var now = new Date();
var midnight = new Date();
midnight.setHours(0,0,0,0);
//Get number of minutes that passed since midnight:
var myTime = Math.floor((now.getTime() - midnight.getTime()) / 60000);
//For Testing Purposes.
console.log(myTime + ' minutes passed since midnight.');

function minutes(hour, min) {
    return hour * 60 + min;
}

//All the periods throughout my school day.
var periods = [
    { start: minutes( 7, 45), end: minutes( 8, 34) },
    { start: minutes( 8, 38), end: minutes( 9, 30) },
    { start: minutes( 9, 34), end: minutes(10, 23) },
    { start: minutes(10, 27), end: minutes(11, 16) },
    { start: minutes(11, 20), end: minutes(12, 38) },
    { start: minutes(12, 42), end: minutes(13, 31) },
    { start: minutes(13, 35), end: minutes(14, 25) },
];

function myFunction(myTime, periods) {
    periods.every(function (period, i) {
        if (myTime < period.start) {
            if (i == 0) {
                console.log('School has not started yet');
            } else {
                var timeLeft = period.start - myTime;
                console.log("There are " + timeLeft + 
                            " minutes left until period " + (i+1) + " starts.");
            }
        } else if (myTime < period.end) {
            var timeLeft = period.end - myTime;
            console.log("There are " + timeLeft + 
                        " minutes left until period " + (i+1) + " is over.");
        } else if (i == periods.length - 1) {
            console.log('School has finished for today');
        } else {
            return true; // keep looking for the right period
        }
    });
}

myFunction(myTime, periods);
&#13;
&#13;
&#13;

答案 1 :(得分:0)

在我写这篇文章的时候睡着了,但如果你将每个概念分解成一口大小的块,它可能会更容易。此外,如果您可以使用 Number ,请不要使用 String ,它会让您的生活更轻松;

在您编写的内容中从日期获取 Numbers 的有用功能

const now = new Date();
const day = now.getDay();
const hh = now.getHours();
const mm = now.getMinutes();

所以你可以写下这样的东西,其中下面的概念被分解成他们自己的块

  • hh:mm 时间,与其他时间hhmm
  • 的距离有多远
  • 一段时间段,名称,它离它有多远(直到/从那里)以及它是否正在进行中
  • 时间表,星期几,时段列表
class TimeTableTime {
    constructor(hh, mm) {
        this.hh = hh;
        this.mm = mm;
    }
    until(hh, mm) { // ms
        return (this.hh - hh) * 60 * 60 * 1000
            + (this.mm - mm) * 60 * 1000;
    }
}

class TimeTableSlot {
    constructor(startHh, startMm, endHh, endMm, name) {
        this.start = new TimeTableTime(startHh, startMm);
        this.end = new TimeTableTime(endHh, endMm);
        this.name = name;
    }
    until(hh, mm) {
        return this.start.until(hh, mm);
    }
    since(hh, mm) {
        return -this.end.until(hh, mm);
    }
    current(hh, mm) {
        return this.start.until(hh, mm) <= 0
            && this.end.until(hh, mm) > 0;
    }
}
const timetable = {
    0: [], // Sunday
    1: [
        new TimeTableSlot(7, 45, 8, 34, 'Period 1'),
        new TimeTableSlot(8, 34, 8, 38, 'Period 1 Transition'),

        new TimeTableSlot(8, 38, 9, 30, 'Period 2'),
        new TimeTableSlot(9, 30, 9, 34, 'Period 2 Transition')
    ],
    6: [] // Saturday
};
// populate other days with example data from Monday
(function () {
    let i;
    for (i = 2; i < 6; ++i) timetable[i] = timetable[1];
}());
// Ideally, you'd also make TimeTable a class and these would be on it's prototype
function current() {
    const now = new Date();
    const day = now.getDay();
    const hh = now.getHours();
    const mm = now.getMinutes();

    return timetable[day].filter(slot => slot.current(hh, mm));
}

function next() {
    const now = new Date();
    const day = now.getDay();
    const hh = now.getHours();
    const mm = now.getMinutes();

    let delta = Infinity;
    return timetable[day].reduce((soonest, testSlot) => {
        let delta2 = testSlot.until(hh, mm);
        if (delta2 < 0) {
            return soonest;
        }
        if (delta2 === delta) {
            soonest.push(testSlot);
            return soonest;
        }
        if (delta2 < delta) {
            delta = delta2;
            return [testSlot];
        }

        return soonest;
    }, []);
}