Disable timezone in my JavaScript random date generator

I have a code where I am trying to generate random dates, but the timezone is always included and I'm not sure how to remove it. Can someone assist me with this? I am new to coding, so any help would be greatly appreciated! Thank you.

var startDate = new Date("1990-01-01"); 
var endDate = new Date("2022-01-01");

function formatDate(date) {
  const year = date.getFullYear();
  /* getMonth returns dates from 0, so add one */
  const month = date.getMonth() + 1;
  const day = date.getDate();
  
  return `${year}-${month < 10 ? '0' : ''}${ month }-${ day < 10 ? '0' : '' }${day}`
}

var getDateArray = function(start, end) {

  return new Date(
    start.getTime() + Math.random() * (end.getTime(0) - start.getTime(0))
  );

}
var dateArr = getDateArray(startDate, endDate);

console.log(dateArr);

Answer №1

If you utilize the formatDate function provided in your code snippet, it should effectively eliminate any timezone data.

var startDate = new Date("1990-01-01"); //YYYY-MM-DD
var endDate = new Date("2022-01-01"); //YYYY-MM-DD

function formatDate(date) {
  const year = date.getFullYear();
  /* getMonth returns dates from 0, so add one */
  const month = date.getMonth() + 1;
  const day = date.getDate();
  
  return `${year}-${month < 10 ? '0' : ''}${ month }-${ day < 10 ? '0' : '' }${day}`
}

var getDateArray = function(start, end) {

  return formatDate(new Date(
    start.getTime() + Math.random() * (end.getTime(0) - start.getTime(0))
  ));

}
var dateArr = getDateArray(startDate, endDate);


console.log(dateArr);

Incorporating your formatDate function within the getDateArray function ensures that the result no longer includes timezone details.

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

Creating an iterative function to add nodes to a Binary Search Tree in JavaScript

Having trouble getting my add/insert method for a BST in JS to work properly. Check out my code below: function Node(value) { this.value = value; this.left = null; this.right = null; } function BinarySearchTree() { this.root = null; ...

Design a clear button for MUI autocomplete feature

I'm currently working on a project where I need to implement a button that clears the MUI autocomplete value and removes the data from the UI. Although I've managed to delete the data from the UI with the code provided, the onChange value remain ...

instructions for transferring image filenames to a database with the help of multer and express

While utilizing multer and express to upload a photo, everything appears to be functioning correctly. However, I am encountering issues with sending the image name to the mongoose database. The images are successfully uploaded to the upload directory. Alt ...

Encountering invalid parameters while attempting to utilize the track.scrobble service from the Last.Fm API in a Node.js application

After successfully completing the Last.Fm authentication process following the instructions provided here, I received the session key without any issues. However, my attempts to make an authenticated POST request to the track.scrobble method of the Last.Fm ...

Establish a many-to-many relationship in Prisma where one of the fields is sourced from a separate table

I'm currently working with a Prisma schema that includes products, orders, and a many-to-many relationship between them. My goal is to store the product price in the relation table so that I can capture the price of the product at the time of sale, re ...

Error encountered when attempting to retrieve data from a JSON object

I'm in the process of extracting some information from a JSON object, but I've encountered a confusing issue. The JSON object structure is as follows: { "status": "success", "data": { "image": null, "video": null, "author": null, "publisher": "M ...

How to retrieve the input value in React Autosuggest

I recently began my journey into learning JavaScript and React. Currently, I am working on creating a simple table with material design. The goal is to be able to add rows to the table through a form popup and delete rows using an icon within each row. On ...

What is the method to retrieve the image's value after dropping it onto the droppable area?

I have implemented a drag and drop feature using jQuery, and I am trying to extract the value of an image and insert it into a database. Additionally, I want to update and remove the image value when it is removed from the droppable area. How can I achie ...

Sending an Angular scope variable to JavaScript

I am working with an angular scope variable that is being used in ng-repeat. My goal is to create a chart that starts from a specific date, ends at another date, and includes a marker for the current day. I am utilizing loader.js for drawing the charts in ...

Scrolling a div automatically without affecting its parent element

On a page, I have a scrollable list of items that can be updated with a PUT request. Once the update is successful, another network request is made to fetch the updated list. The goal is to automatically highlight the recently updated item in the list. Al ...

Error in Node.js: Module 'chai' not found

I've been experimenting with an example I found on Completed this step: $ npm install -g mocha Resulted in: C:\Windows\system32>npm install -g mocha npm WARN deprecated Jade has been renamed to pug, please install the latest version o ...

Discover multiple keys within a new Map object

Our usual approach involves creating a new Map like this: const hash = new Map() hash.set(key,value) To retrieve the information, we simply use: hash.get(specificKey) An advantage of using Map is that we have flexibility in choosing keys and values. Cur ...

Exploring the method to find all corresponding keys within deeply nested objects

Within my 'const', I have an array filled with objects. Each object contains a key called 'image' which holds the value for 'url' as illustrated below. const images =[ { "image": { "url& ...

fade out row upon successful deletion using ajax

My code includes an AJAX function to delete items by row. In my table, there is a column with the action to perform the deletion. Here is the AJAX function: //delete detail function deleteDetail(id_po_req_detail) { var tr = ...

Jesting around with mocking console.error leads to test failures

The Issue: Currently, I am working on testing React components using Jest and Enzyme. In order to check for properties in development, I have incorporated the prop-types module. The prop-types module relies on console.error to alert when mandatory props a ...

Creating Isolated SCSS for an external Vue.js Component Library

I have implemented a vue-select library and the CSS file needs to be imported into App.vue for global use. Afterwards, I customized it using scoped SCSS. Here is the resulting HTML and CSS: <v-select :options="array" v-model="selectedIte ...

Unexpected Error: Null value prevents accessing 'getElementsByClassName' property in HTML, along with Troubleshooting Inactive Button Behavior in HTML

Can someone identify the error in my HTML code? I keep getting an "Uncaught TypeError: Cannot read property 'getElementsByClassName' of null" error on the second line of the script tag. Additionally, my implemented active header code is not funct ...

Tips for organizing the items in an array in a specific order

Seeking advice on sorting array objects in JavaScript based on input sequence. Given an array input [abc, xyz, 123], the output array object looks like: [ { "label" : "positive", "id" : "abc" }, { "label" : "positive", "id" : "xyz" }, { "label" : "negativ ...

Unable to retrieve a collection within Angular-Meteor framework

I'm currently facing challenges while working with data from a Collection in Angular-Meteor as I struggle to access it successfully. Inside lib/collections.js, I have defined the collection: UserMeta = new Mongo.Collection('userMeta'); In ...

If the href attribute is set to "#" or the target attribute is set to "_blank", then the <a> request cannot be prevented

I have a registration site that triggers a warning window whenever a user clicks on any link during the registration process. However, I want to exclude <a> tags with href="#" and target="_blank" attributes from this behavior. Essentially, I want to ...