用JavaScript计算上周的开始日期(不是-7天前)

时间:2015-09-11 03:08:54

标签: javascript date datetime

我有一个活动记录列表,这些记录都有一个日期并显示在活动流中。

我想根据记录创建日期在记录之间根据它们所属的日期组插入分隔符DIV。

我想要显示带有本周文本的DIV,它会占用当前周的开始日期(不是过去7天),然后我可以显示自该日期以来创建的所有记录。< / p>

我还想为上周做一个,这意味着我需要获取上周开始日期和上周结束日期的日期值。或者开始日期+7就可以了。

然后我有一个JavaScript库来比较日期范围。我可以传递一个开始日期和结束日期,并查看第三个日期是否在日期范围内,这将让我知道我的哪些记录将显示在每个日期部分下。

所以我在计算:

时请求帮助

一周的开始日期是大多数日历或星期一的星期日

  • 当前周开始日期
  • 上周开始日期和结束日期

如果我可以提供帮助,我也不愿意使用体积庞大的Moment.js库进行这些日期操作。

感谢您的帮助

下面这张图片展示了我想要通过日期部分来实现我的记录......

enter image description here

DateTime比较的日期库,并检查日期是否介于两个日期之间!

// Source: http://stackoverflow.com/questions/497790
var dates = {
  convert: function(d) {
    // Converts the date in d to a date-object. The input can be:
    //   a date object: returned without modification
    //  an array      : Interpreted as [year,month,day]. NOTE: month is 0-11.
    //   a number     : Interpreted as number of milliseconds
    //                  since 1 Jan 1970 (a timestamp)
    //   a string     : Any format supported by the javascript engine, like
    //                  "YYYY/MM/DD", "MM/DD/YYYY", "Jan 31 2009" etc.
    //  an object     : Interpreted as an object with year, month and date
    //                  attributes.  **NOTE** month is 0-11.
    return (
      d.constructor === Date ? d :
      d.constructor === Array ? new Date(d[0], d[1], d[2]) :
      d.constructor === Number ? new Date(d) :
      d.constructor === String ? new Date(d) :
      typeof d === "object" ? new Date(d.year, d.month, d.date) :
      NaN
    );
  },
  compare: function(a, b) {
    // Compare two dates (could be of any type supported by the convert
    // function above) and returns:
    //  -1 : if a < b
    //   0 : if a = b
    //   1 : if a > b
    // NaN : if a or b is an illegal date
    // NOTE: The code inside isFinite does an assignment (=).
    return (
      isFinite(a = this.convert(a).valueOf()) &&
      isFinite(b = this.convert(b).valueOf()) ?
      (a > b) - (a < b) :
      NaN
    );
  },
  inRange: function(d, start, end) {
    // Checks if date in d is between dates in start and end.
    // Returns a boolean or NaN:
    //    true  : if d is between start and end (inclusive)
    //    false : if d is before start or after end
    //    NaN   : if one or more of the dates is illegal.
    // NOTE: The code inside isFinite does an assignment (=).
    return (
      isFinite(d = this.convert(d).valueOf()) &&
      isFinite(start = this.convert(start).valueOf()) &&
      isFinite(end = this.convert(end).valueOf()) ?
      start <= d && d <= end :
      NaN
    );
  },

  // Subtract number of months from current month
  // dates.subtractMonth(1)
  subtractMonth: function(numberOfMonths) {
    //var d = this;
    var d = new Date();
    d.setMonth(d.getMonth() - numberOfMonths);
    d.setDate(1);
    return d;
  }

};

///////////////////////////////////////////////////////////////////////////

我的样本JSON数据,其日期将与其他日期进行比较,以确定何时将日期部分标题注入循环

var task_activities = [{"id":1,"name":"record 1","date_time":"1\/5\/2015"},{"id":2,"name":"record 2","date_time":"1\/9\/2015"},{"id":3,"name":"record 3","date_time":"1\/13\/2015"},{"id":4,"name":"record 4","date_time":"1\/17\/2015"},{"id":5,"name":"record 5","date_time":"1\/21\/2015"},{"id":6,"name":"record 6","date_time":"1\/25\/2015"},{"id":7,"name":"record 7","date_time":"1\/29\/2015"},{"id":8,"name":"record 8","date_time":"2\/1\/2015"},{"id":9,"name":"record 9","date_time":"2\/5\/2015"},{"id":10,"name":"record 10","date_time":"2\/9\/2015"},{"id":11,"name":"record 11","date_time":"2\/13\/2015"},{"id":12,"name":"record 12","date_time":"2\/17\/2015"},{"id":13,"name":"record 13","date_time":"2\/21\/2015"},{"id":14,"name":"record 14","date_time":"2\/25\/2015"},{"id":15,"name":"record 15","date_time":"2\/29\/2015"},{"id":16,"name":"record 16","date_time":"3\/1\/2015"},{"id":17,"name":"record 17","date_time":"3\/5\/2015"},{"id":18,"name":"record 18","date_time":"3\/9\/2015"},{"id":19,"name":"record 19","date_time":"3\/13\/2015"},{"id":20,"name":"record 20","date_time":"3\/17\/2015"}];



// Loop over each Task Activity record and generate HTML
$.each(task_activities, function(i, activity) {
    console.log(activity.date_time);
}); // end each

0 个答案:

没有答案