What is the best way to increase a JavaScript date by 1 UTC day?

Is there a way to use javascript to take a non-UTC date, add 1 UTC day to it, reset the time to zero, and then convert it to an ISO string?


new Date().toISOString()

2017-10-10T16:00:49.915Z


Required UTC Datestring

2017-10-11T00:00:00.000Z

Answer №1

In the code snippet below, I first retrieve the date in milliseconds and then add a whole day in milliseconds to it. Next, I divide the total by one day in milliseconds, truncate the result, and multiply it again by a day in milliseconds.

var d = new Date('2017-10-10T16:00:49.915Z');

function nextDayUTC(d) {
  var aDay = 1440 * 60 * 1000;
  var d2 = new Date( Math.trunc((d.getTime() + aDay)/aDay)*aDay);
  return d2;
}

function nextDayLocal(d) {
  //basically set to start of the day
  //add 36 hrs, this pretty much ensures next day
  //add then reset the hours back to 0
  var hr36 = 36 * 60 * 60 * 1000;
  var d2 = new Date(d);
  d2.setHours(0,0,0,0); 
  d2.setTime(d2.getTime() + hr36);
  d2.setHours(0,0,0,0);
  return d2;
}

console.log(d)
console.log("Next Day UTC");
console.log(nextDayUTC(d).toISOString());
console.log("Next Day Local");
console.log(nextDayLocal(d).toString());

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

Leveraging various libraries in React

My query revolves around a React application where I have integrated Material UI for only two components. Despite installing the entire Material UI library, will this lead to an increased bundle size for my application? Or does the build process only inc ...

Implementing the jquery mobile data-native-menu feature in a select element dynamically generated from jeditable

Greetings! I have encountered an issue where the data-native-menu="false" instruction works correctly when directly placed in a select element, but doesn't work when added to a select generated by JavaScript (using the Jeditable plugin). You can view ...

I'm having trouble with AngularJS routes not functioning properly when accessed directly. I am using html5 mode in conjunction with

When accessing URLs directly in my Angular app either through the address bar or from external links, all routes default back to the home page. However, when navigating within the app, routes work as expected. I have come across similar issues on Stack Ov ...

Utilizing JQuery to modify the element's id and trigger a function upon clicking

There is an element with a specific ID that I have changed. However, even after changing the ID and clicking on the element with the new ID, it still triggers the function associated with the old ID. $('#1st').click(function(){ $('#1s ...

What is the method to determine the length of an animation in Three.js?

Is there a way to retrieve the duration of an animation in three.js? For example, consider the following code snippet: var mixer = new THREE.AnimationMixer(gltf.scene) gltf.animations.forEach((clip)) => { let animation = mixer.clipAction(clip) animati ...

404 error occurs when AngularJS removes hash symbol from URLs

I successfully eliminated the hash from my Angular JS website using the following code snippet: $locationProvider.html5Mode(true); Now, the previously displayed URL has been replaced with . The issue I'm facing is that when I do a hard refresh (Ct ...

Using AJAX to transfer a variable from a range slider to a PHP query

I'm currently trying to pass a JavaScript variable from a range slider to a PHP query. The goal is to have the front-page of my Wordpress theme display all posts tagged with the value selected on the range slider without having to refresh the page. To ...

Combining Google app endpoints with a phonegap app: Step-by-step guide

I've been working on a Phonegap client application and have developed all the necessary web services using Google Endpoints. However, I am facing an issue with using the API. In my index.html file, there is this script: <head><script> ...

Tips for closing an event when switching between tabs or windows

I'm in the process of developing an online test portal within my Angular application, and I need to figure out how to automatically close/end the session when a user navigates (opens a new tab or window) to a different page. Is there a way to achieve ...

the function does not wait for the DOM content to finish loading

As I execute the code, an error occurs stating that setting innerText of Null is not allowed. I understand that this is because my function runs before the DOM is completely loaded, but I am unsure of how to resolve this issue. Attempts have been made usi ...

What is the best way to apply multiple conditions to filter an array efficiently?

Looking to filter an array with multiple conditions? I have several checkboxes pre-checked and I need to filter based on start_date and end_date: future: start_date is after today past: end_date is after today's date in_progress: start_date is to ...

Steps to get the vehicle approaching the creature

Recently, I ventured into the realm of game development and created a car game where the vehicle moves automatically. However, when the car collides with a monster, I want it to start moving towards the monster instead. After some research, I decided to in ...

I understand the reason behind the unexpected { token error, but I'm unsure of how to resolve it when my PHP script needs to transmit a collection of data to JavaScript

I am currently utilizing this JavaScript fetch code to retrieve data from PHP async sendRequest(selectValue=this.selectValue){ const fetchResponse = await fetch('/server/getLastWords.php?select='+selectValue); const fetchJSON = await fe ...

I'm not sure why the CSS attribute that restricts character numbers exists

https://i.sstatic.net/dkRt3.png The Divtag appears to be malfunctioning due to insufficient properties. I am currently unsure of how to resolve this issue and would appreciate any insights or opinions. <div class="col boardsBox" style="margin-right:0. ...

Incorporate a smooth transition effect to a dynamically resizing div button

In my current setup, I have 3 buttons that can toggle between different visible divs. In the future, I may add more buttons to switch between additional divs. At the moment, the first div is active (display set to block), while all other divs are hidden ( ...

How does Backbone.js tackle error handling when receiving messages from Rails?

Can anyone using backbone.js lend a hand? How can error messages from a rails app be encoded when integrating with backbone.js? For instance, how should flash messages like "record not found" be handled? While errors are usually defined on the client sid ...

``There seems to be an issue with the Express app when trying to access it

I have set up an express app that I want other devices on the same WIFI network to access without requiring internet connectivity. The main computer hosting the app is assigned with a fixed IP address: 192.168.1.60 In my server.js file, I have included t ...

Tips for extracting a segment from an Object within an Array

I am looking to extract the datetime value from each element in the array below: [<time pubdate class="dt-updated" datetime="2015-07-09T11:50:32+0000" title="Time posted: 09 Jul 2015, 11:50:32 (UTC)" aria-label="Posted on 09 Jul">09 Jul</time> ...

What is the best way to utilize findOneAndUpdate() in Mongoose while maintaining a consistent value?

I have a programming challenge where I need to increment one value if condition x is true, and another value if condition y is true. The issue I'm facing is that my current system also resets the value that is not being updated back to 0, which is not ...

Why is it that vue-cookies are functioning properly in one view, yet not in another?

In my Login.vue file, I successfully set a token cookie using the 'vue-cookies' package. The cookie is properly set and working. However, when I try to access the same cookie in another view, I encounter the following error in the Chrome console ...