javascript日期+;7天

javascript日期+;7天,javascript,jquery,date,Javascript,Jquery,Date,这个脚本有什么问题 当我将时钟设置为2011年4月29日时,它会在一周的输入中添加2011年4月36日!但是正确的日期应该是2011年6月5日 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.get

这个脚本有什么问题

当我将时钟设置为2011年4月29日时,它会在一周的输入中添加2011年4月36日!但是正确的日期应该是2011年6月5日

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);
    }
这里有两个问题:

  • seven_date
    是一个数字,不是日期<代码>29+7=36
  • getMonth
    返回该月的从零开始的索引。因此,添加一个只会得到当前月份的数字 像这样的

    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)
    

    使用Date对象的方法会很方便

    e、 g:

    myDate = new Date();
    plusSeven = new Date(myDate.setDate(myDate.getDate() + 7));
    
    var-date=新日期();
    date.setDate(date.getDate()+7);
    
    控制台日志(日期)获取未来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
    );
    
    var天数=7天;
    变量日期=新日期();
    var res=date.setTime(date.getTime()+(天*24*60*60*1000));
    var d=新日期(res);
    变量月份=d.getMonth()+1;
    var day=d.getDate();
    变量输出=d.getFullYear()+'/'+
    (月<10?'0':'')+月+'/'+
    (天<10?'0':'')+天;
    $('#txtEndDate').val(输出);
    
    您可以为以下示例添加或增加星期几,希望这对您有所帮助。让我们看看

            //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();           
    
    var future=new Date();//今日约会
    future.setDate(future.getDate()+7);//加7天
    var finalDate=future.getFullYear()+'-'+((future.getMonth()+1)<10?'0':'')+(future.getMonth()+1)+'-'+future.getDate();
    控制台日志(最终日期)在一行中:

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

    无需声明

    返回时间戳

    new Date().setDate(new Date().getDate() + 7)
    
    返回日期

    new Date(new Date().setDate(new Date().getDate() + 7))
    
    目前js用户

    var startdate = "20.03.2014"; or new Date().getDate()
    var new_date = moment(startdate, "DD-MM-YYYY").add(7, 'days');
    
    对于纯js

    date.setDate(date.getDate() + 7);
    

    d.getDate()
    会给你一个整数…现在它不再是某种日期对象了…我建议你只需通读你在那里做的事情,然后在纸上完成它,你们的错误对你们来说是显而易见的。嗯,好吧,那个么你们知道如何使用最佳实践来实现这一点吗?一般来说,你们会想在date对象上使用一些setter方法(对于这个月,你们也会在十二月遇到类似的问题)。然后,您可以将结果Date对象转换为字符串。它返回1303980894137/4/2011shuffles off,即unix时间戳-然后您可以将其转换为人类可读的值-猜测这是最佳做法第二段中的赋值是多余的,“Date”在调用SetTimes时更新这很好,它似乎做了我想做的事情,但它返回的日期如下:Fri May 06 2011 11:07:01 GMT+0200,但我“希望日期的格式为dd/mm/年,如:06/05/2011什么现代社会使用超过31天?@CodeMonkey超过1个月;)这并不能回答这个问题。若要评论或要求作者澄清,请在其帖子下方留下评论。-他想知道为什么他的代码不起作用。他还暗暗地想知道如何在7天后得到这个日期。我提供了答案。@nurdyguy这是以什么方式“批评或要求澄清”?虽然一个解释最少的代码示例可能不是一个很好的答案,但它是一个答案。看,让这家伙休息一下,这是一个非常好和简单的一行,没有混乱的临时工作。谢谢!这正是我需要的。为了我的目的稍微修改一下,我需要一个划时代的时间,所以我使用了:Math.floor((Date.now()+7*24*60*60*1000)/1000)——希望这对其他人也有用!
    date.setDate(date.getDate() + 7);