【问题标题】:Get all dates in a range of 2 weeks获取 2 周范围内的所有日期
【发布时间】:2013-07-24 18:41:14
【问题描述】:

我正在开发 Titanium 中的应用程序,我需要获取 2 周内的所有日期。

例如,今天的日期是 2013-24-07,我需要像这样获取 2013-07-08 之前的所有日期:

var dates = [];

dates[0] = '2013-24-07';
dates[1] = '2013-25-07';
dates[2] = '2013-26-07';
dates[3] = '2013-27-07';
dates[4] = '2013-28-07';
dates[5] = '2013-29-07';
dates[6] = '2013-30-07';
dates[7] = '2013-31-07';
dates[8] = '2013-01-08';

等等……

我用我找到的代码here 创建了一个test,但我无法让它工作。

非常感谢任何帮助,

谢谢

【问题讨论】:

    标签: javascript date


    【解决方案1】:

    试试这样的:

    // create a extension for Dates like this
    Date.prototype.addDays = function(days)
    {
        var dat = new Date(this.valueOf());
        dat.setDate(dat.getDate() + days);
        return dat;
    }
    

    并像这样使用它:

    // create the array
    var dates = [];
    
    // define the interval of your dates
    // remember: new Date(year, month starting in 0, day);
    var currentDate = new Date(); // now
    var endDate = new Date(2013, 07, 07); // 2013/aug/07
    
    // create a loop between the interval
    while (currentDate <= endDate)
    {
       // add on array
       dates.push(currentDate);
    
       // add one day
       currentDate = currentDate.addDays(1);
    }
    

    在此方法的最后,dates 数组将包含间隔的日期。

    看这里:http://jsfiddle.net/5UCh8/1

    【讨论】:

      【解决方案2】:

      我搜索了你的问题,发现了这段代码:

      var start = new Date("02/05/2013");
      var end = new Date("02/10/2013");
      
      while(start < end){
         alert(start);           
      
         var newDate = start.setDate(start.getDate() + 1);
         start = new Date(newDate);
      }
      

      如果您需要帮助,请告诉我。 祝你好运

      【讨论】:

        【解决方案3】:
        var start = Date.now();
        var days = 14;
        var dates = []
        for(var i=0; i<days; i++)
            dates.push(new Date(start + (i * 1000 * 60 * 60 * 24)).toDateString());
        alert(dates)
        

        【讨论】:

        • 太棒了!感谢您的快速回答,在 jsFiddle 中对其进行了测试,效果很好。
        猜你喜欢
        • 1970-01-01
        • 2022-01-05
        • 1970-01-01
        • 1970-01-01
        • 2012-02-26
        • 2014-09-17
        • 2021-01-25
        • 2011-07-30
        • 1970-01-01
        相关资源
        最近更新 更多