Calculating Future Dates in JavaScript by Adding Days

I am looking to add 20 days to the current date and present the output in the mm/dd/yyyy format. Presently, with today being 05/15/2014, the result produced is 05/35/2014, which is clearly an invalid date.

var currentYear = new Date();
alert((currentYear.getMonth() + 1) + "/" + (currentYear.getDate() + 20) + "/" + currentYear.getFullYear());

Answer №1

To add a specific number of days to the current date in JavaScript, utilize the setDate() method of the Date object.

var myDate = new Date();
myDate.setDate(myDate.getDate() + 20);

alert((myDate.getMonth() + 1) + "/" + (myDate.getDate()) + "/" +   myDate.getFullYear());

The above code will output a date of:

6/4/2014

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

Implement Meteor authentication with Google OAuth

I'm attempting to set up a basic login button for my Meteor app using google oauth. I followed these steps: mrt create accounts mrt add accounts-google mrt add accounts-ui I removed the default html/css/js files and instead added: client/index.html ...

Confusion arises from conflicting Vue component script indentation guidelines

As I work on setting ESLint rules for my new Vue project, I am extending both eslint-plugin-vue and airbnb. All is well except for one issue - the indentation of the tag inside Vue components. The usual accepted format looks like this: <script> ex ...

What rules should be followed in Python when it comes to using nested parentheses in functions?

Currently in the process of deleting my account from this platform, I am intentionally adding unnecessary content to this post. Please refrain from restoring the previous content. - Original Poster ...

The linking process in AngularJS is encountering difficulties when trying to interact with

I've already looked at similar questions, and my code seems correct based on the answers provided. It's a very simple beginner code. <html ng-app=""> <head> <title>Assignment</title> <script src=" ...

Implementing context menus on the Material-UI DataGrid is a straightforward process that can enhance the user experience

I am looking to enhance my context menus to be more like what is demonstrated here: Currently, I have only been able to achieve something similar to this example: https://codesandbox.io/s/xenodochial-snow-pz1fr?file=/src/DataGridTest.tsx The contextmenu ...

Tips for integrating Excel files with NestJS

I'm in the process of developing a REST API that will utilize a third-party API to retrieve specific status information. The URLs needed for this API are stored in an Excel file, which is a requirement for this use case. My goal is to extract the URLs ...

"AngularJS offers a unique way to include alternative text in placeholders and titles using

I need to implement an input field (<input..>) with a placeholder and title for tooltip that functions in the following way: There is a variable called "myString" which can either be undefined/empty or contain a string. When empty, a default string ...

Steps for setting an array of ObjectIds in Mongoose

Running tests on the fixture population in mongoose. const mongoose = require('mongoose') const { ObjectId } = require('mongodb'); const app = require('express')() mongoose.connect('<connection fine>', { useN ...

Ways to display form choices that are influenced by other form selections

In my form, the user is supposed to choose a specific item at the end. As they fill in the initial options, the subsequent fields below change dynamically. Here is an example: Type: { t1:{ Number of X:{ 1:{...} 2:{...} ...

Removing a method signature from a type that extends a function returning any in TypeScript

I am currently developing an API in typescript where I aim to return a function that includes multiple functions as properties, one of which is the same function. My approach to achieving this includes: type MainFunc = () => PublicInterface type Publi ...

Iterate through the JSON data values using a loop and showcase each one by presenting them in individual cards

I'm having trouble determining which type of loop to use in this situation because I am still learning jQuery and JS. On the result page, I have set up cards to separate each piece of data from the JSON file, but I am unsure how to actually display th ...

Using Vue: Dynamically pass components to router-view within dynamically loaded components

For a specific URI, I am looking to display a different layout on the same route by using different components based on whether the user is on mobile or desktop. I want to avoid cluttering the PageCommon (layout component) with route path checks. In the a ...

Combine PHP, jQuery, and AJAX to retrieve multiple values simultaneously

I have been using jQuery AJAX for my projects. When I make an AJAX call to a PHP page, it normally returns a single value to the success function of AJAX. However, I am now looking to retrieve multiple data individually. How can I achieve this? This is th ...

How can I generate multiple DIV elements within a for loop using JavaScript?

Can a series of unique divs be created using a for loop? for (var i = 0, n = 4; i < n; i++) { var divTag = document.createElement("div"); divTag.id = "div"i; divTag.innerHTML = Date(); document.body.appendChild(divTag); } Is it expected that this scri ...

What's the most effective method for isolating elements in HTML to prevent one element from impacting another?

I recently created a password form in HTML with some nice functions. However, after making a small adjustment, the CSS is causing the elements to shift when clicking on different input boxes. This results in the labels on the form moving slightly up and do ...

A role requiring coordinates x, y, and z with significant values

http://jsfiddle.net/eho5g2go/ var offset=10000000; ... camera.position.set(offset,offset,400); mesh.position.set(offset-300,offset,0); camera.lookAt(mesh.position); ... animate(); The "offset" variable is crucial for determining the camera and mesh p ...

What is the best way to adjust the width of an image?

Can someone assist me with a script for implementing an auto read more feature on my blog? I have specified values for image thumbnail height and width in the script, but they are not being applied to my blog. Any help would be appreciated. To view my blo ...

How can the .pre() middleware function in Mongoose be utilized?

I'm curious about the use cases for mongoose .pre('validate') and .pre('save'). I understand their functionality, but I'm struggling to think of specific scenarios where I would need to utilize them. Can't all necessary a ...

Merging Angular with jQuery: Organizing jQuery into a distinct file leads to the error message 'Property 'top' cannot be read because it is undefined.'

My project is based on AngularJS, and I've been trying to incorporate a jQuery code for a sticky sidebar feature. Strangely enough, when I tested my code on Plunkr, it works perfectly fine. However, when I try to implement it in my actual project, it ...

The visibility of a ThreeJS mesh disappears when its center moves out of the camera's view

Struggling to develop a map featuring various meshes, I've encountered an issue where meshes disappear when the center of the mesh is out of the camera view. Check out this gif demonstrating the problem: Currently using THREE.WebGLRenderer 71, is th ...