Я только что сделал следующий код и имею несколько проблем с ним. Вся идея проекта заключается в том, чтобы определить, сколько времени вы оставили в школьный период или до начала периода. Сначала я сохранил время пользователя в качестве переменной и выделил текущие часы и минуты, используя подстроку. Вот код для этого.JavaScript Subtract Time Diffrence
//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;
}
Благодаря
Zach
Мое предложение состоит в том, что вы угробили все это и начать все сначала с помощью [moment.js] (HTTP: // momentjs.com/). – JohnnyHK
@JohnnyHK Будет ли это намного проще? –
@ zmalter99 Да, это будет –