如何提醒上周的开始和结束日期

时间:2014-05-03 09:42:55

标签: javascript jquery

如何提醒前几周的所有开始和结束日期。

var weekCount = 0;
$(".week-prev").live('click', function() {
    var weekdate = new Date();
    var fromweek = weekdate.setTime(weekdate.getTime() - 7 * 24 * 60 * 60 * 1000);
    var toweek = weekdate.setTime(weekdate.getTime() - (weekdate.getDay() ? weekdate.getDay() : 7) + weekCount * 24 * 60 * 60 * 1000);
    var prevweekstart = new Date(fromweek);
    var prevweekends = new Date(toweek);
    prevweekstart = prevweekstart.toLocaleString(),
            prevweekends = prevweekends.toLocaleString(),
            between = [];
    alert(prevweekstart);
    alert(prevweekends);
});

1 个答案:

答案 0 :(得分:0)

我建议在Javascript

中使用moment.js进行所有日期操作
moment().startOf('week');    // set to the first day of this week, 12:00 am
moment().startOf('isoWeek'); // set to the first day of this week according to ISO 8601, 12:00 am
moment().endOf('week');    // set to the last day of this week, 12:00 am
moment().endOf('isoWeek'); // set to the last day of this week according to ISO 8601, 12:00 am

从版本2.1.0开始,#startOf('week')时刻使用区域设置感知周开始日。

对于前一周的示例,您将使用:

moment().subtract('weeks', 1).startOf('week');