Tips for fetching date information every ten minutes

Is there a method you know of to create date data that increases every 10 minutes and store it in an array?


let datesArray: Array<Date> = new Array();

let firstDate: Date = new Date('2019-05-03T06:00:00');
let secondDate: Date = new Date('2019-05-03T06:00:00');
let thirdDate: Date = new Date('2019-05-03T06:00:00');

firstDate.setMinutes(firstDate.getMinutes() + 10);
console.log(firstDate);
secondDate.setMinutes(secondDate.getMinutes() + 10);
console.log(secondDate);
thirdDate.setMinutes(secondDate.getMinutes() + 10);
console.log(thirdDate);


while(firstDate.getTime() <= 6){ // Starting from 6:00, looking for intervals of 10 minutes until (7:00). Is it possible to go up to 7:00?
    firstDate.setMinutes(firstDate.getMinutes() + 10);
    console.log(firstDate);
}

Answer №1

    let currentDate = new Date();
    let futureDate = new Date();

    //Adjust the value in the line below to change the amount of time added.
    futureDate.setTime(currentDate.getTime() + (10 * 60 * 1000));

    console.log(futureDate);

Answer №2

It's a great concept you have there. The key is to implement this within a loop and generate a new Date instance for each iteration:

const startDate = new Date('2019-05-03T06:00:00');
const numberOfItems = 12;

const dates = Array.from({ length: numberOfItems }, (_, i) => {
  const nextDate = new Date(startDate);
  nextDate.setMinutes(startDate.getMinutes() + i * 10);
  return nextDate;
});

console.log(dates.map(String));

You might find it beneficial to encapsulate this logic within a function that suits your requirements:

function dateIncrements(startDate, incrementMinutes, numberOfItems) {
  return Array.from({ length: numberOfItems }, (_, i) => {
    const nextDate = new Date(startDate);
    nextDate.setMinutes(startDate.getMinutes() + i * incrementMinutes);
    return nextDate;
  });
}

const dates = dateIncrements(new Date('2019-05-03T06:00:00'), 10, 6);
console.log(dates.map(String));

Answer №3

Have you considered utilizing the setInterval function instead? It appears that employing this native method could enhance the efficiency and cleanliness of your code...

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

Storing form information within the view

What is the best way to transfer data between views in AngularJS? I tried using $rootScope but it's not working as expected. ...

Error with hyperlinks preventing redirection when clicking on the menu

$(document).ready(function () { $('.mobile-nav-button').on('click', function() { $( ".mobile-nav-button .mobile-nav-button__line:nth-of-type(1)" ).toggleClass( "mobile-nav-button__line--1"); $( ".mobile-nav ...

In Chrome, there is a flicker in the jQuery animated fade out before the callback function is triggered

I created a small feature that allows users to click on a text element, causing it to animate and fly to a specific location before disappearing. Check out this demo of the functionality. Below is the code snippet from the click handler (written in coffe ...

Utilize nested object models as parameters in TypeScript requests

Trying to pass request parameters using model structure in typescript. It works fine for non-nested objects, but encountering issues with nested arrays as shown below: export class exampleModel{ products: [ { name: string, ...

Ways to conceal components of an external widget on my site

I'm attempting to add a chat widget to my website and I'm looking to hide specific elements within the widget. The chat function is provided by Tidio, but I believe this applies to most widgets. My main goal is to conceal a button that minimize ...

Do the Firebase v3 JS libraries have a debug version that is not obfuscated or minified?

In the earlier versions before 3.0, there used to be a "debug" version of the library that could be accessed by adding -debug to the filename. For instance, v2.4.2 had an obfuscated variation along with a debug variant. However, it seems like v3.1.0 only ...

Problem with white screen in Cocos2d-html5

I'm experiencing an issue with my cocos2d html5 game on the Tizen platform. While it runs perfectly on PC and all browsers without any errors, on Tizen it only displays a white screen. It seems like the update loop is functioning correctly, but there ...

add styling to elements contained within a specified list of URLs

I'm not confident in my CSS/HTML/jQuery skills but I believe what I want to achieve may require JavaScript/jQuery. I want to apply a specific style to an element only when a certain URL is accessed on my website. Since I am using ASP.NET and one temp ...

Content loaded using AJAX paired with lightbox2

I have implemented the Lightbox2 script on my website and I am loading the content of my page using AJAX. However, I am facing an issue as there seems to be no function available to attach new images or initialize Lightbox2 after an AJAX request. How can I ...

What is the best way to manage a promise in Jest?

I am encountering an issue at this particular section of my code. The error message reads: Received promise resolved instead of rejected. I'm uncertain about how to address this problem, could someone provide assistance? it("should not create a m ...

Error message: RefererNotAllowedMapError - Google Maps API has encountered an issue with

I've integrated the Google Places API into my website to display a list of addresses, but I'm encountering the error detailed below. Encountered the following error when trying to use Google Maps API: RefererNotAllowedMapError https://developers ...

"Utilize the simplewebauthn TypeScript library in combination with a password manager for enhanced security with passkeys

After using Passkey in TypeScript with the library , I noticed that it saved the passkey in my browser Keychain or Mac Keychain. However, I would like my password manager (such as Dashlane, 1Password, etc.) to save it similar to this site How can I confi ...

Exploring Sprite Range with Raycasting in Three.js

I am working on a 3D scene using three.js and adding sprites, but I want to accurately determine the distance between the camera and a sprite when I click on the screen. To achieve this, I am utilizing a Raycaster. However, when clicking on the sprite, th ...

The value of req.body becomes null following a post request

I've been working on creating a contact form with the help of nodemailer. When trying to submit it using the fetch API, I encountered an issue where req.body is being returned as undefined. Below is the frontend code snippet: form.onsubmit = functio ...

Clicking on the response from the GET request to view the data in a separate browser tab

I'm having difficulty figuring out how to use Ajax and JQuery to send a GET request with data parameters and display the response in a new browser tab. Here's an example of what I'm trying to accomplish: $('#test-button').on(&apos ...

What is the best way to import API Endpoints from various directories in an Express Server?

I have been using a script to load my API endpoints like this: readdirSync('./routes/api').map((r) => app.use( `/api/v1/${r.split('.')[0]}`, require(`./routes/api/${r.split('.')[0]}`) ) ); This script reads eve ...

Incorporating a module with the '@' symbol in a Node, Express, and Typescript environment

I recently started using Node and Typescript together. I came across a file where another module is imported like this import { IS_PRODUCTION } from '@/config/config';. I'm confused about how the import works with the @ symbol. I could real ...

Invoke actions when clicking outside of components

Currently, I have a HeaderSubmenu component that is designed to show/hide a drop-down menu when a specific button is clicked. However, I am now trying to implement a solution where if the user clicks anywhere else in the application other than on this drop ...

Error: Unable to access the applicant's ID as it is undefined

I'm currently facing an issue with passing parameters from server.js to humanresources.js in a login request. Although the params are successfully printed out in server.js, they appear as "undefined" once passed on to the function in human resources.j ...

The hashKey is not generating a new value when pushing a new object in an Angular application

I have come across an issue related to generating new $$hashKey within the following code snippet: this.list = '[{ "body": "asdf", "tag": "resolved", "time": "2147483647", "id": "51" }, { "body": "asdf", "tag": "undone", " ...