【问题标题】:javascript date + 7 daysjavascript 日期 + 7 天
【发布时间】:2011-08-10 03:25:31
【问题描述】:

这个脚本有什么问题?

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

【问题讨论】:

  • d.getDate() 给你一个整数...此时它不再是某种日期对象了...
  • 我不得不建议您只是阅读您在那里所做的事情并在纸上完成它,您的错误对您来说是显而易见的。

标签: javascript jquery date


【解决方案1】:

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

console.log(date);

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

【讨论】:

  • 现代社会用什么超过31天?
  • @CodeMonkey 超过 1 个月的 ;)
【解决方案2】:

无需声明

返回时间戳

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

返回日期

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

【讨论】:

    【解决方案3】:

    这样的?

    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)
    

    【讨论】:

    • 这是 unix 时间戳 - 您可以将其转换为人类可读的值 - 猜测这是最佳实践
    • 第二段的赋值是多余的,'date'在你调用setTime时更新了
    【解决方案4】:

    获取未来 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
    );
    

    【讨论】:

    • 这很好,它似乎可以满足我的要求,但它会返回这样的日期 Fri May 06 2011 11:07:01 GMT+0200 但我喜欢日期采用这种格式 dd /mm/year 像这样:06/05/2011
    【解决方案5】:

    一行:

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

    【讨论】:

    • 这并没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方留下评论。 - From Review
    • 他想知道为什么他的代码不起作用。含蓄地他也想知道如何从现在得到 7 天后的日期。我提供了答案。
    • @nurdyguy 这个“批评或要求澄清”以什么方式?虽然简单解释的代码示例可能不是一个很好的答案,但它一个答案。见when to flag an answer as “not an answer”?
    • 让这个家伙休息一下,这是一个非常好的和简单的单线,可以在没有凌乱的临时工的情况下完成工作。
    • 谢谢!这正是我所需要的。为了我的目的稍微修改了它,我需要一个纪元时间,所以我使用了: Math.floor((Date.now() + 7 * 24 * 60 * 60 * 1000)/1000) - 希望这对某人有用其他的也一样!
    【解决方案6】:

    使用 Date 对象的方法 will 可能会派上用场。

    例如:

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

    【讨论】:

    • 它返回 1303980894137/4/2011
    • 在自我厌恶中洗牌
    【解决方案7】:
    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);
    

    【讨论】:

      【解决方案8】:

      您可以为以下示例添加或增加星期几,希望这对您有所帮助。让我们看看....

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

      【讨论】:

        【解决方案9】:

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

        【讨论】:

          【解决方案10】:

          这里有两个问题:

          1. seven_date 是一个数字,而不是日期。 29 + 7 = 36
          2. getMonth 返回从零开始的月份索引。因此,添加一个只会让您获得当前月份的编号。

          【讨论】:

          • 嗯,好的,那么关于如何使用最佳实践来实现这一点的任何想法?
          • 通常你会想要在日期对象上使用一些setter方法(对于月份来说,你会在十二月遇到类似的问题)。然后您可以将生成的 Date 对象转换为字符串。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-10-03
          • 2012-10-17
          • 1970-01-01
          • 2021-06-28
          • 2023-02-23
          • 1970-01-01
          相关资源
          最近更新 更多