Establishing the ISO date time to the beginning of the day

When working with a Date retrieved from a Mongo query in ISO format, the goal is to compare it with today's date. To accomplish this, a new Date is created and its time is set to zero:

var today = new Date();
today.setHours(0,0,0,0); //Sat Jan 20 2018 00:00:00 GMT-0800 (PST)

While this method sets the time to zero, the issue arises when converting it to an ISO string for comparison:

console.log(today.toISOString()); //2018-01-20T08:00:00.000Z

The hour remains at 08 instead of being set to zero. This discrepancy makes setting the hour to zero challenging.

Answer №1

let currentDate = new Date();
currentDate.setUTCHours(0,0,0,0);

document.getElementById('date').innerHTML = currentDate.toISOString();
<label>Current Date: (ISO String)</label>
<div id="date">
<div>

currentDate.setUTCHours(0,0,0,0);

This code snippet will set the UTC hours to midnight for the current date and display it in ISO format.

Answer №2

The solution provided above is accurate. I was assigned the task of identifying bookings for the present, past, and future dates by querying the MongoDB database. By utilizing the ISO String method mentioned above, I successfully resolved the issue at hand. Much gratitude for your assistance.

 const { past, present, future } = req.query;

  let today = new Date();
  today.setUTCHours(0, 0, 0, 0);
  today.toISOString();

  let bookings;

  if (past === 'past') {
    bookings = await Booking.find({
      booking_user: req.user.userId,
      booking_checkin: { $lt: today },
    }).sort('-booking_status : cancelled');
  } else if (present === 'present') {
    bookings = await Booking.find({
      booking_user: req.user.userId,
      booking_checkin: { $eq: today },
    }).sort('-booking_status : cancelled');
  } else if (future === 'future') {
    bookings = await Booking.find({
      booking_user: req.user.userId,
      booking_checkin: { $gt: today },
    }).sort('-booking_status : cancelled');
  } else {
    bookings = await Booking.find({
      booking_user: req.user.userId,
    }).sort('-booking_status : cancelled');
  }

  res.status(StatusCodes.OK).json({ count: bookings.length, bookings });

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

I am interested in utilizing props to send a variable to the view

Looking for assistance with passing the variable tmp_sell to my view. Here is the code: <p @tmp_sell="getTmpSell" >?</p> <input ref="q_subtotal" placeholder="Subtotal" @tmp_sell="getTmpSell" i ...

Seeking help with a Javascript regex inquiry

I am currently utilizing JavaScript regex for the following task: I have gathered the HTML content from a page and stored it within a string. Now, I aim to identify all URLs present on this page. For instance, if the document includes-- <script src = ...

Conceal a div and label after a delay of 5 seconds with a JavaScript/jQuery function in C#

Here is a sample div: <div class="alert alert-success alert-dismissable" runat="server" visible="false" id="lblmsgid"> <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> ...

Setting up TypeScript in an Angular 2 project and integrating Facebook login

Currently, I am in the process of familiarizing myself with Angular 2 and typescript. Although things have been going smoothly so far, I have hit a roadblock while attempting to implement a Facebook login message. In my search for a solution, I stumbled up ...

What is the method for reaching a service in a different feature module?

Currently, I am utilizing Angular 2/4 and have organized my code into feature modules. For instance, I have a Building Module and a Client Module. https://i.stack.imgur.com/LvmkU.png The same structure applies to my Client Feature Module as well. Now, i ...

Using a combination of ASP.NET webpage and JavaScript, a dynamic webpage with a repe

I am new to using VB and integrating JavaScript. I have a simple question, but I'm having trouble figuring it out for some reason. I am struggling to properly construct an IF-statement. Can you please help me? I have searched and googled, but have no ...

Showing data in json format using Angular

I have designed a data table that showcases a list of individuals along with their information. However, when I click on the datatable, it keeps opening a chat box displaying the details of the last person clicked, overriding all other chat boxes. 1. Is t ...

Placing a list item at the beginning of an unordered list in EJS with MongoDB and Node.js using Express

What I've done: I already have knowledge on how to add an LI to UL, but it always goes to the bottom. What I'm trying to achieve: Add an LI to the top so that when my div.todos-wrapper (which has y-oveflow: hidden) hides overflow, the todos you a ...

Can the root directory of a node module be customized or specified?

When publishing a node module with source files in a src directory, users typically need to specify the full path from the module when importing a file into their project. For example: Directory Structure: my-module --src ----index.js ----something-else ...

MongoDB experiencing issues with executing dynamic $and queries

$SearchParam = $_GET["tag"]; $strcount = substr_count($SearchParam, ' '); // Here is the code that generates the tag strings and creates the search parameter dynamically if ($strcount > 0) { $fruitQuery = "'$" ."and' => array("; f ...

Adding an item to the collection

When I log my cartProducts within the forEach() loop, it successfully stores all the products. However, if I log my cartProducts outside of the loop, it displays an empty array. var cartProducts = []; const cart = await CartModel .fin ...

Secure your DOM's href attribute from prying eyes

I have a display page that shows all of our reports in the following format: https://i.sstatic.net/lNzH8.png When hovering over a report, it displays the file's URL (where it is located on our server). I want to prevent users from accessing this inf ...

Using JavaScript and node.js, make sure to wait for the response from socket.on before proceeding

My task involves retrieving information from the server on the client side. When a client first connects to the server, this is what happens: socket.on('adduser', function(username){ // miscellaneous code to set num_player and other variabl ...

I am attempting to integrate a datetimepicker onto my website, but I keep encountering an error. Can

Once upon a time, my website had a perfectly functioning datetimepicker. However, one day it suddenly stopped working. After some investigation, I realized that the JavaScript file I was referencing had been taken down. Determined to fix the issue, I visit ...

Instructions for implementing a Back button that takes you directly to the text or link you clicked on to view an image

My goal is to have multiple anchor links within text, each linking to a specific image on the same page. Once the user views the image, I want them to be able to click a 'Back' button that will take them back to where they left off in the text. ...

Issue with the iOS gyroscope detected when rotating specifically around the z-axis

I am facing a unique challenge with an unusual bug and I'm looking for help from anyone who has encountered this issue before or can provide a solution. My current project involves using Javascript to access the gyro on iOS devices, specifically focu ...

Unable to leverage the most recent iteration of three js

Having trouble using the newest version of three.js (r102) in IE. I keep getting an ImageBitMap error (ImageBitMap is not defined). Any tips on how to solve this would be greatly appreciated. Thanks! ...

Using React-Router v6 to pass parameters with React

My App.js file contains all the Routes declarations: function App() { return ( <div className="App"> <Routes> <Route path="/"> <Route index element={<Homepage />} /> ...

Adjust the hue of a Three.js world map

Check out this awesome Three.js demo with a 3D Globe: I've been trying to modify the color of the globe from black to navy blue, but despite my efforts in editing the source files, I haven't been successful in changing its appearance. My unders ...

Can I use AJAX to load an entire PHP file?

My goal is to trigger a PHP file for sending an email when the countdown I created reaches zero. The PHP code contains the email message and does not involve any UI elements. I suspect that my approach may be incorrect, especially since I am not very fami ...