如何使用map函数使用JavaScript查看时间是否在特定时间范围内?
假设我们有主题记录以及计划研究它们的时间-
const scheduleDetails = [
{ subjectName: 'JavaScript', studyTime: '5 PM - 11 PM' },
{ subjectName: 'MySQL', studyTime: '12 AM - 4PM' }
]这是我们可以使用的方法map()。以下是代码-
示例
const scheduleDetails = [
{ subjectName: 'JavaScript', studyTime: '5 PM - 11 PM' },
{ subjectName: 'MySQL', studyTime: '12 AM - 4PM' }
]
function timeToReadSubjectName(scheduleDetails) {
var currentTime = new Date().getHours();
let result = '';
scheduleDetails.map(obj => {
const hourDetails = obj.studyTime.split(' ');
const firstCurrentTime = hourDetails[1] === 'PM' ? 12 : 0;
const secondCurrentTime = hourDetails[4] === 'PM' ? 12 : 0;
if (currentTime > (+hourDetails[0] + firstCurrentTime) &&
currentTime < (+hourDetails[3] + secondCurrentTime)) {
result = obj.subjectName;
};
})
return result;
}
console.log("The Subject which you need to read in this
time="+timeToReadSubjectName(scheduleDetails));要运行上述程序,您需要使用以下命令-
node fileName.js.
在这里,我的文件名为demo128.js。
输出结果
这将产生以下输出-
PS C:\Users\Amit\JavaScript-code> node demo128.js The Subject which you need to read in this time=JavaScript
热门推荐
10 朋友新年祝福语大全 简短
11 新年祝福语简短大方兔年
12 搬新家礼物祝福语简短
13 同学见面花束祝福语简短
14 五一假期祝福语幽默简短
15 离职欢送敬酒祝福语简短
16 对学弟的祝福语简短
17 考老师辞职祝福语简短
18 祝福语驱散霉运的话简短