Comparing current time with a given hour string in JavaScript

Seeking assistance with comparing the hour attribute string Time:"10:50" in JavaScript to the current system hour. For example, if the hour is 10, then run a code in a function. Any guidance on how to achieve this would be greatly appreciated. Thank you.

Answer №1

If you're working with a date string, consider wrapping it in a date object and using the getHours() function on that object.

// Create a new date object 'oldDate' by passing the date string as a parameter
let oldDate = new Date("date string"); 
// Creating another date object 'systemDate' without parameters will use the system time
let systemDate = new Date();

if(oldDate.getHours() == systemDate.getHours())

Alternatively, if you are confident that the response will be in the form of "10:50", this could be a better approach:

let time = "10:50";
let h = parseInt(time.split(":")[0]);

let systemDate = new Date();
if(h == systemDate.getHours())

Answer №2

  • Instantiate a new Date object: let todayDate = new Date();
  • Retrieve the current hour:
    let currentHour = todayDate.getHours();
  • Contrast it with your intended variable.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Connecting Ember controllers with views/elements

As a developer with an Angular background, I am relatively new to Ember. Let's consider a scenario where there are multiple elements, each containing different sets of data. #elem1 10 #elem2 20 #elem3 30 My objective is to link each of these indiv ...

Methods for activating touch-like scrolling through the use of mouse grabbing and dragging

Let me show you a simple illustration of the task I'm attempting to accomplish: <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> ...

Issue with React component not updating

Experiencing an issue in a React code related to nested components within React. Link to code The problem arises from having a parent component called "testing" with two child components, Component1 and Component2, each with Component3 as a sub-child und ...

Is there a way to position my character at the exact center of my mouse click coordinates instead of the top left corner?

In my game, players can click on a 2D HTML5 canvas to set a point for their character to move to. However, I've noticed that when I click, the character appears in the lower right corner of where my mouse is clicked. After some research, I realized th ...

Discovering the initial vacant row within an HTML table with Javascript

Below is a snippet of code that generates a table with 6 rows. The last two rows remain empty. The JavaScript logic in the code correctly determines and displays the total number of rows in the table. I'm currently looking for a way to identify the ...

Creating dynamic HTML and CSS in ASP.NET: A step-by-step guide

I am currently working on integrating a photo album in my asp.net website. I have been able to successfully display 3 images from a specific album by selecting their URLs from the database. However, I am facing an issue when a user has multiple albums. I n ...

Perform a function dynamically once a specific textfield is filled and continuously while filling another textfield

Imagine we have two text fields - one for a first name and one for a surname. As I type in the surname field after already filling out the first name, a function called sug() should provide suggestions or perform some action each time I add another letter ...

What is the best way to establish a new JavaScript data type that can be accessed using both key and index?

Looking to create a unique datatype or object in JavaScript that allows access by both index and key, as well as having a length property to display the number of items in the datatype. Something along the lines of: MyDataType[0].name="John" MyDataType[0 ...

The value could not be retrieved because the input name depends on the array index

To determine the name of the input based on the array index, use the following method: <div id="editAboutSantences<%=i%>" class="edit-container"> <div class="input-container"> <label> content: </label> ...

Showing canvas lines while dragging (using only plain JavaScript, with React.JS if needed)

Is there a way to add lines with two clicks and have them visible while moving the mouse? The line should only be drawn when clicking the left mouse button. Any suggestions on how I can modify my code to achieve this functionality? Currently, the lines are ...

Trouble sliding with Material UI Slider

I've encountered an issue with my Material UI slider - it doesn't slide when clicked and dragged. I followed one of the examples on the Material UI website (https://material-ui.com/components/slider/) and included an onChange function. The values ...

Issues arise when the Angular controller fails to load

I'm experiencing an issue with my Angular controller where the code inside its constructor is not running. Here's a snippet of the relevant pieces: conversationcontrollers.js: var exampleApp = angular.module('exampleApp',[]); console ...

Node.js application encounters a challenge with Swagger UI requirement not being met

My Node.js application utilizes Electron v15.3.1 and includes a Swagger UI for OAS file viewing, specifically version 4.6.1. Due to a security vulnerability in the Swagger UI, I need to upgrade it. Initially, I attempted to resolve the issue by running np ...

Displaying currency format in an input field using AngularJS filter

I'm currently dealing with an input field that looks like this: <input type="text" class="form-control pull-right" ng-model="ceremony.CeremonyFee | number:2"> Although it is displaying correctly, I've noticed that it's disabled. The ...

In my app.post request in node.js and express, the body object is nowhere to be found

Having an issue with node.js and express, trying to fetch data from a post request originating from an HTML file. However, when I log the request, the req.body object appears empty. I've added a console.log(req.body) at the beginning of my app.post(" ...

What could be the reason for my post jQuery Ajax request sending JSON data?

After downloading some code, I came across the following fragment: function FetchCommentsBySessionIDWCF_JSON() { varType = "POST"; varUrl = "service/CommentSessionIDWCFService.svc/FetchCommentsByPost"; varData = ' ...

Configuring a Node application for a production server with Babel

I am in the process of preparing my very first node app for a production server. The build and serve scripts I am using are based on those provided by babel "scripts": { "start": "nodemon --exec babel-node server.js --ignore public/", "build": "b ...

What is preventing me from utilizing the Jquery show() method within a Div's onclick event without incorporating data-toggle="dropdown"?

I am facing an issue with a div that should act like a button. When this div is clicked, another hidden div is supposed to be displayed. However, the behavior is not as expected. I have included the data data-toggle="dropdown" attribute in the div acting a ...

An issue has arisen with the TypeScript function classes within the React Class, causing a compile error to be thrown

I am currently in the process of transitioning a React object from being a function to a class. This change is necessary in order to save the state and bind specific functions that I intend to pass to child components. Unfortunately, during compilation, I ...

jqGrid display/hide columns options dialogue box

I am looking to implement a feature that allows users to toggle columns in my jqGrid. I came across a module for this purpose, but unfortunately, it is no longer supported in jqGrid 4.x. I have searched for a replacement module without success. Are there ...