比较两次范围 JavaScript
Compare two times range JavaScript
我想比较两次并将结果显示为布尔值。我为此创建了一个函数。这是工作。但是当比较“AM”和“PM”时输出是错误的。
let Time = "11:25 PM";
let currentTime = "10:00 AM";
console.log(isFutureTime(currentTime, Time));
//Output should be true or false
function isFutureTime(currentTime, checkTime) {
if (checkTime.split(" ")[1] == currentTime.split(" ")[1]) {
if (parseInt(currentTime.split(":")[0]) < parseInt(checkTime.split(":")[0])) {
return true;
}
else if (parseInt(currentTime.split(":")[0]) == parseInt(checkTime.split(":")[0])) {
if (parseInt(currentTime.split(":")[1].substring(0, 2)) <= parseInt(checkTime.split(":")[1])) {
return true;
} else {
return false;
}
} else {
return false;
}
}
else {
return false;
}
}
您需要检查 currentTime
是上午还是下午,return true
如果是下午。
...
return currentTime.split(" ")[1] === 'PM';
...
因为您已经检查了两次是否相等,所以您不需要检查 checkTime
。
你为 currentTime.split(" ")[1]
声明一个变量以提高效率。
let Time = "11:25 PM";
let currentTime = "10:00 AM";
console.log(isFutureTime(currentTime, Time));
//Output should be true or false
function isFutureTime(currentTime, checkTime) {
if (checkTime.split(" ")[1] == currentTime.split(" ")[1]) {
if (parseInt(currentTime.split(":")[0]) < parseInt(checkTime.split(":")[0])) {
return true;
} else if (parseInt(currentTime.split(":")[0]) == parseInt(checkTime.split(":")[0])) {
if (parseInt(currentTime.split(":")[1].substring(0, 2)) <= parseInt(checkTime.split(":")[1])) {
return true;
} else {
return false;
}
} else {
return false;
}
} else {
return currentTime.split(" ")[1] === 'PM';
}
}
或者,Javascript Date
可以本地解析您正在使用的时间格式。所以,我只需要连接一个日期字符串并为这两个值构造一个 Date
实例。然后比较一下。
function isFutureTime(currentTime, checkTime) {
let date = new Date()
date = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`
return new Date(`${date} ${currentTime}`) > new Date(`${date} ${checkTime}`)
}
const currentTime = '10:00 AM';
const futureTime = '11:25 PM';
const pastTime = '03:15 AM';
console.log(futureTime, '=', isFutureTime(currentTime, futureTime))
console.log(pastTime, '=', isFutureTime(currentTime, pastTime))
我想比较两次并将结果显示为布尔值。我为此创建了一个函数。这是工作。但是当比较“AM”和“PM”时输出是错误的。
let Time = "11:25 PM";
let currentTime = "10:00 AM";
console.log(isFutureTime(currentTime, Time));
//Output should be true or false
function isFutureTime(currentTime, checkTime) {
if (checkTime.split(" ")[1] == currentTime.split(" ")[1]) {
if (parseInt(currentTime.split(":")[0]) < parseInt(checkTime.split(":")[0])) {
return true;
}
else if (parseInt(currentTime.split(":")[0]) == parseInt(checkTime.split(":")[0])) {
if (parseInt(currentTime.split(":")[1].substring(0, 2)) <= parseInt(checkTime.split(":")[1])) {
return true;
} else {
return false;
}
} else {
return false;
}
}
else {
return false;
}
}
您需要检查 currentTime
是上午还是下午,return true
如果是下午。
...
return currentTime.split(" ")[1] === 'PM';
...
因为您已经检查了两次是否相等,所以您不需要检查 checkTime
。
你为 currentTime.split(" ")[1]
声明一个变量以提高效率。
let Time = "11:25 PM";
let currentTime = "10:00 AM";
console.log(isFutureTime(currentTime, Time));
//Output should be true or false
function isFutureTime(currentTime, checkTime) {
if (checkTime.split(" ")[1] == currentTime.split(" ")[1]) {
if (parseInt(currentTime.split(":")[0]) < parseInt(checkTime.split(":")[0])) {
return true;
} else if (parseInt(currentTime.split(":")[0]) == parseInt(checkTime.split(":")[0])) {
if (parseInt(currentTime.split(":")[1].substring(0, 2)) <= parseInt(checkTime.split(":")[1])) {
return true;
} else {
return false;
}
} else {
return false;
}
} else {
return currentTime.split(" ")[1] === 'PM';
}
}
或者,Javascript Date
可以本地解析您正在使用的时间格式。所以,我只需要连接一个日期字符串并为这两个值构造一个 Date
实例。然后比较一下。
function isFutureTime(currentTime, checkTime) {
let date = new Date()
date = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`
return new Date(`${date} ${currentTime}`) > new Date(`${date} ${checkTime}`)
}
const currentTime = '10:00 AM';
const futureTime = '11:25 PM';
const pastTime = '03:15 AM';
console.log(futureTime, '=', isFutureTime(currentTime, futureTime))
console.log(pastTime, '=', isFutureTime(currentTime, pastTime))