JavaScript 减去时差

标签 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)大于开始日且小于结束日时起作用。这是因为 startday 存储为我的上学时间的开始时间,而 endday 存储为我的上学时间的结束时间。如果用户当前时间不满足此要求,则会转到 else 语句,该语句表示学校尚未开始。

如果您想查看并提出任何建议,这里是其余的代码。

//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;
}

谢谢

扎克

最佳答案

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

此外,如果您使用数组来存储不同的周期,您的代码将会减少重复。

以下是您可以执行的操作:

//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);

关于JavaScript 减去时差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41353666/

相关文章:

javascript - Node.js 内存泄漏?

javascript - 无法读取嵌套函数内 'get' $http 的属性

javascript - 输入文件上传多个文件在移动设备上不起作用

javascript - 实时多人游戏-区域广播

javascript - 在 Angular JS 中,如何将指令属性中的数据注入(inject)模板?

javascript - 与backbone.js 的命名约定(在rails 上)

javascript - 寻找一个插件来更快更容易地引用所有js文件

javascript getdisplaymedia 以更高分辨率记录

javascript - 具有链式 AJAX 调用的条件逻辑

javascript - 访问从 HOC 返回的组件 Prop - Jest with React