Подтвердить что ты не робот

Javascript date + 7 дней

Что случилось с этим script?

Когда я установил свои часы, чтобы сказать 29/04/2011, он добавляет 36/4/2011 в недельный ввод! но правильная дата должна быть 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);
    }
4b9b3361

Ответ 1

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

var dateMsg = date.getDate()+'/'+ (date.getMonth()+1) +'/'+date.getFullYear();
alert(dateMsg);

Ответ 2

Что-то вроде этого?

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

конвертировать в дату еще раз:

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

или, альтернативно:

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)

Ответ 3

Простым способом получения даты x дней в будущем является увеличение даты:

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

Обратите внимание, что это изменяет предоставленный объект даты, например

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
);

Ответ 4

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);

Ответ 5

Вы можете добавить или увеличить день недели для следующего примера и надеяться, что это будет полезно для вас. Давайте посмотрим....

        //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();           

Ответ 6

Две проблемы здесь:

  • seven_date - это число, а не дата. 29 + 7 = 36
  • getMonth возвращает индекс на основе нуля месяца. Таким образом, добавив, вы просто получаете номер текущего месяца.

Ответ 7

Использование методов объекта Date будет может пригодиться.

например:

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