Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to use the map function to see if a time is in a certain time frame with JavaScript?
In JavaScript, you can use the map() function to iterate through schedule records and check if the current time falls within a specific time frame. This is useful for determining which activity or subject you should be working on at any given moment.
Example Data Structure
Let's start with a simple schedule containing subject names and their study times:
const scheduleDetails = [
{ subjectName: 'JavaScript', studyTime: '5 PM - 11 PM' },
{ subjectName: 'MySQL', studyTime: '12 AM - 4 PM' }
]
Using map() to Check Time Frames
Here's how to use map() to find which subject matches the current time:
const scheduleDetails = [
{ subjectName: 'JavaScript', studyTime: '5 PM - 11 PM' },
{ subjectName: 'MySQL', studyTime: '12 AM - 4 PM' }
];
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));
The Subject which you need to read in this time = JavaScript
How It Works
The function works by:
- Getting the current hour using
new Date().getHours() - Using
map()to iterate through each schedule item - Parsing the time string by splitting on spaces
- Converting PM times by adding 12 hours
- Checking if current time falls between start and end times
Improved Version with Better Time Handling
Here's an enhanced version that handles edge cases better:
const scheduleDetails = [
{ subjectName: 'JavaScript', studyTime: '5 PM - 11 PM' },
{ subjectName: 'MySQL', studyTime: '12 AM - 4 PM' },
{ subjectName: 'Python', studyTime: '10 PM - 2 AM' }
];
function findCurrentSubject(scheduleDetails) {
const currentHour = new Date().getHours();
const results = scheduleDetails.map(item => {
const [startTime, endTime] = item.studyTime.split(' - ');
// Parse start time
const [startHour, startPeriod] = startTime.split(' ');
let start24 = parseInt(startHour);
if (startPeriod === 'PM' && start24 !== 12) start24 += 12;
if (startPeriod === 'AM' && start24 === 12) start24 = 0;
// Parse end time
const [endHour, endPeriod] = endTime.split(' ');
let end24 = parseInt(endHour);
if (endPeriod === 'PM' && end24 !== 12) end24 += 12;
if (endPeriod === 'AM' && end24 === 12) end24 = 0;
// Check if current time is within range
let isInRange = false;
if (start24 <= end24) {
// Same day range
isInRange = currentHour >= start24 && currentHour < end24;
} else {
// Overnight range
isInRange = currentHour >= start24 || currentHour < end24;
}
return isInRange ? item.subjectName : null;
}).filter(subject => subject !== null);
return results.length > 0 ? results[0] : 'No subject scheduled';
}
console.log("Current subject to study:", findCurrentSubject(scheduleDetails));
Current subject to study: JavaScript
Key Points
-
map()transforms each schedule item by checking time conditions - Time parsing converts 12-hour format to 24-hour format for easier comparison
- The improved version handles overnight schedules (e.g., 10 PM - 2 AM)
- Filter is used to remove null values and get actual matches
Conclusion
Using map() with time frame checking allows you to dynamically determine which scheduled activity should be active at the current time. This approach is particularly useful for scheduling applications and time management systems.
