
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to use the map function to see if a time is in a certain time frame with JavaScript?
Let’s say, we have subject records with the time when we are planning to study them −
const scheduleDetails = [ { subjectName: 'JavaScript', studyTime: '5 PM - 11 PM' }, { subjectName: 'MySQL', studyTime: '12 AM - 4PM' } ]
Here is how we can use the map(). Following is the code −
Example
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));
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo128.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo128.js The Subject which you need to read in this time=JavaScript
- Related Articles
- How to use time() with local time in Android sqlite?
- How to plot Time Zones in a map in Matplotlib
- How do I trigger a function when the time reaches a specific time in JavaScript?
- How To Check If The Time Difference Is Greater Than A Specific Time?
- How to use time zone in a JSP?
- How to redirect website after certain amount of time without JavaScript?
- How to write a function to get the time spent in each function in Python?
- How to measure the time taken by a function in Java?
- How To Check If Time Is Greater Than Or Less Than A Specific Time In Excel?
- How to check if a time series is stationary in R?
- How to convert a Unix timestamp to time in JavaScript?
- How to measure time taken by a function in C?
- How to use my object like an array using map function in JavaScript?
- How to set current time to some other time in JavaScript?
- How to create everyday notifications at certain time in Android?

Advertisements