javascript date + 7 days

JavascriptJqueryDate

Javascript Problem Overview


What's wrong with this script?

When I set my clock to say 29/04/2011 it adds 36/4/2011 in the week input! but the correct date should be 6/5/2011

var d = new Date();
var curr_date = d.getDate();
var tomo_date = d.getDate()+1;
var seven_date = d.getDate()+7;
var curr_month = d.getMonth();
curr_month++;
var curr_year = d.getFullYear();
var tomorrowsDate =(tomo_date + "/" + curr_month + "/" + curr_year);
var weekDate =(seven_date + "/" + curr_month + "/" + curr_year);
{
jQuery("input[id*='tomorrow']").val(tomorrowsDate);
jQuery("input[id*='week']").val(weekDate);
    }

Javascript Solutions


Solution 1 - Javascript

var date = new Date();
date.setDate(date.getDate() + 7);

console.log(date);

And yes, this also works if date.getDate() + 7 is greater than the last day of the month. See MDN for more information.

Solution 2 - Javascript

Without declaration

To return timestamp

new Date().setDate(new Date().getDate() + 7)

To return date

new Date(new Date().setDate(new Date().getDate() + 7))

Solution 3 - Javascript

Something like this?

var days = 7;
var date = new Date();
var res = date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
alert(res);

convert to date again:

date = new Date(res);
alert(date)

or alternatively:

date = new Date(res);

// hours part from the timestamp
var hours = date.getHours();

// minutes part from the timestamp
var minutes = date.getMinutes();

// seconds part from the timestamp
var seconds = date.getSeconds();

// will display time in 10:30:23 format
var formattedTime = date + '-' + hours + ':' + minutes + ':' + seconds;
alert(formattedTime)

Solution 4 - Javascript

In One line:

new Date(Date.now() + 7 * 24 * 60 * 60 * 1000)

Solution 5 - Javascript

The simple way to get a date x days in the future is to increment the date:

function addDays(dateObj, numDays) {
  return dateObj.setDate(dateObj.getDate() + numDays);
}

Note that this modifies the supplied date object, e.g.

function addDays(dateObj, numDays) {
   dateObj.setDate(dateObj.getDate() + numDays);
   return dateObj;
}

var now = new Date();
var tomorrow = addDays(new Date(), 1);
var nextWeek = addDays(new Date(), 7);

alert(
    'Today: ' + now +
    '\nTomorrow: ' + tomorrow +
    '\nNext week: ' + nextWeek
);

Solution 6 - Javascript

Using the Date object's methods will could come in handy.

e.g.:

myDate = new Date();
plusSeven = new Date(myDate.setDate(myDate.getDate() + 7));

Solution 7 - Javascript

var days = 7;
var date = new Date();
var res = date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));

var d = new Date(res);
var month = d.getMonth() + 1;
var day = d.getDate();

var output = d.getFullYear() + '/' +
    (month < 10 ? '0' : '') + month + '/' +
    (day < 10 ? '0' : '') + day;

$('#txtEndDate').val(output);

Solution 8 - Javascript

You can add or increase the day of week for the following example and hope this will helpful for you.Lets see....

        //Current date
        var currentDate = new Date();
        //to set Bangladeshi date need to add hour 6           

        currentDate.setUTCHours(6);            
        //here 2 is day increament for the date and you can use -2 for decreament day
        currentDate.setDate(currentDate.getDate() +parseInt(2));

        //formatting date by mm/dd/yyyy
        var dateInmmddyyyy = currentDate.getMonth() + 1 + '/' + currentDate.getDate() + '/' + currentDate.getFullYear();           

Solution 9 - Javascript

var future = new Date(); // get today date
future.setDate(future.getDate() + 7); // add 7 days
var finalDate = future.getFullYear() +'-'+ ((future.getMonth() + 1) < 10 ? '0' : '') + (future.getMonth() + 1) +'-'+ future.getDate();
console.log(finalDate);

Solution 10 - Javascript

Two problems here:

  1. seven_date is a number, not a date. 29 + 7 = 36
  2. getMonth returns a zero based index of the month. So adding one just gets you the current month number.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser472285View Question on Stackoverflow
Solution 1 - Javascriptadam77View Answer on Stackoverflow
Solution 2 - JavascriptEmanuelView Answer on Stackoverflow
Solution 3 - JavascriptezmilhouseView Answer on Stackoverflow
Solution 4 - JavascriptMarcoLeView Answer on Stackoverflow
Solution 5 - JavascriptRobGView Answer on Stackoverflow
Solution 6 - JavascripttrickwallettView Answer on Stackoverflow
Solution 7 - JavascriptOmid MatouriView Answer on Stackoverflow
Solution 8 - JavascriptRejwanul RejaView Answer on Stackoverflow
Solution 9 - JavascriptGenius in troubleView Answer on Stackoverflow
Solution 10 - JavascriptJakub HamplView Answer on Stackoverflow