Javascript:获取前一周的周一和周日

时间:2012-12-03 10:51:16

标签: javascript

我使用以下脚本获取前一周的周一(第一个)和周日(最后一个):

var curr = new Date; // get current date
var first = curr.getDate() - curr.getDay() - 6; // Gets day of the month (e.g. 21) - the day of the week (e.g. wednesday = 3) = Sunday (18th) - 6
var last = first + 6; // last day is the first day + 6
var startDate = new Date(curr.setDate(first));
var endDate = new Date(curr.setDate(last));

如果上周一和周日也在同一个月,这样可以正常工作,但我今天才注意到,如果今天是十二月,而上一个星期一是十一月,它就不起作用。

我是一个JS新手,还有其他方法可以获得这些日期吗?

8 个答案:

答案 0 :(得分:11)

您可以通过获取本周一的周一并减去7天来获得上周一。星期天将是前一天,所以:

var d = new Date();

// set to Monday of this week
d.setDate(d.getDate() - (d.getDay() + 6) % 7);

// set to previous Monday
d.setDate(d.getDate() - 7);

// create new date of day before
var sunday = new Date(d.getFullYear(), d.getMonth(), d.getDate() - 1);

2012-12-03我得到:

Mon 26 Nov 2012
Sun 25 Nov 2012

这就是你想要的吗?

// Or new date for the following Sunday
var sunday = new Date(d.getFullYear(), d.getMonth(), d.getDate() + 6);

给出了

Sun 02 Dec 2012

通常,您可以通过添加和减去年,月和日来操纵日期对象。该对象将自动处理负值,例如

var d = new Date(2012,11,0)

将创建2012-11-30的日期(注意月份为零,因此11月份为12月)。也:

d.setMonth(d.getMonth() - 1); // 2012-10-30

d.setDate(d.getDate() - 30);  // 2012-09-30

答案 1 :(得分:5)

如果您不想使用外部库,则应使用时间戳。我创建了一个解决方案,您可以从当前日期减去60 * 60 * 24 * 7 * 1000(即604800000,即1周毫秒)并从那里开始:

var beforeOneWeek = new Date(new Date().getTime() - 60 * 60 * 24 * 7 * 1000)
  , day = beforeOneWeek.getDay()
  , diffToMonday = beforeOneWeek.getDate() - day + (day === 0 ? -6 : 1)
  , lastMonday = new Date(beforeOneWeek.setDate(diffToMonday))
  , lastSunday = new Date(beforeOneWeek.setDate(diffToMonday + 6));

答案 2 :(得分:4)

您可以使用像moment.js这样的库。 请参阅subtract方法http://momentjs.com/docs/#/manipulating/subtract/

答案 3 :(得分:3)

提到了一些答案,但没有人写过这个简单的方法:

moment().day(-13) // Monday last week
moment().day(-7) // Sunday last week

.day设定了一周的工作日,所以今天的哪一天并不重要,只有一周是重要的。

答案 4 :(得分:1)

使用Moment,您可以执行以下操作

  var lastWeek          = moment().isoWeek(moment().subtract(1,'w').week());
  var mondayDifference  = lastWeek.dayOfYear() - lastWeek.weekday() + 1;
  var sundayDifference  = mondayDifference - 1;

  var lastMonday        = moment().dayOfYear(mondayDifference);
  var lastSunday        = moment().dayOfYear(sundayDifference  );

答案 5 :(得分:1)

这是查找一周中任何一天的通用解决方案。

function getParticularDayTimestamp(lastWeekDay) {
  var currentWeekMonday = new Date().getDate() - new Date().getDay() + 1;
  return new Date().setDate(currentWeekMonday - lastWeekDay);
}

console.log(getParticularDayTimestamp(7)) // for last week monday
console.log(getParticularDayTimestamp(1)) // for last week sunday
console.log(getParticularDayTimestamp(14)) // for last to last week monday
console.log(getParticularDayTimestamp(8)) // for last to last week sunday

答案 6 :(得分:0)

这里有一个多用途功能:

 function getThe(numOfWeeks, weekday, tense, fromDate) { 
 // for instance: var lastMonday = getThe(1,"Monday","before",new Date()) 
      var targetWeekday = -1;  
      var dateAdjustment = clone(fromDate);  
      var result = clone(fromDate);

      switch (weekday) {
        case "Monday": targetWeekday = 8; break;
        case "Tuesday": targetWeekday = 2; break;
        case "Wednesday": targetWeekday = 3; break;
        case "Thursday": targetWeekday = 4; break;
        case "Friday": targetWeekday = 5; break;
        case "Saturday": targetWeekday = 6; break;
        case "Sunday": targetWeekday = 7;
      }

      var adjustment = 7 * (numOfWeeks - 1);
      if (tense == "after") adjustment = -7 * numOfWeeks;

      dateAdjustment.setDate(fromDate.getDate() - targetWeekday);
      var weekday = dateAdjustment.getDay();

      result.setDate(fromDate.getDate() - weekday - adjustment);
      result.setHours(0,0,0,0);
      return result;
  }

你可以找到"克隆(obj)"在下一篇文章中发挥作用:https://stackoverflow.com/a/728694/6751764

答案 7 :(得分:0)

就这么简单。

var today = new Date();
var sunday = new Date(this.today.getFullYear(), this.today.getMonth(), this.today.getDate() - this.today.getDay());````