javascript 日期 + 7 天

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5741632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:38:46  来源:igfitidea点击:

javascript date + 7 days

javascriptjquerydate

提问by user472285

What's wrong with this script?

这个脚本有什么问题?

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

当我将时钟设置为 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);
    }

回答by adam77

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

console.log(date);

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

是的,如果date.getDate() + 7大于该月的最后一天,这也有效。有关更多信息,请参阅 MDN

回答by ezmilhouse

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)

回答by RobG

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

获取未来 x 天日期的简单方法是增加日期:

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

回答by Omid Matouri

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

回答by Rejwanul Reja

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

回答by Genius in trouble

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

回答by trickwallett

Using the Date object's methods willcould come in handy.

使用 Date 对象的方法 将要可以派上用场。

e.g.:

例如:

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

回答by Jakub Hampl

Two problems here:

这里有两个问题:

  1. seven_dateis a number, not a date. 29 + 7 = 36
  2. getMonthreturns a zero based index of the month. So adding one just gets you the current month number.
  1. seven_date是一个数字,而不是日期。 29 + 7 = 36
  2. getMonth返回该月的基于零的索引。所以添加一个只会让你得到当前的月份数。

回答by Emanuel

Without declaration

无需声明

To return timestamp

返回时间戳

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

To return date

返回日期

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

回答by MarcoLe

In One line:

在一行中:

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