Javascript:获取前一周的星期一和星期日
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13681702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript: get Monday and Sunday of the previous week
提问by user18577
I am using the following script to get Monday (first) and Sunday (last) for the previous week:
我正在使用以下脚本获取前一周的星期一(第一个)和星期日(最后一个):
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));
This works fine if last Monday and Sunday were also in the same month, but I just noticed today that it doesn't work if today is December and last Monday was in November.
如果上周一和周日也是在同一个月,这可以正常工作,但我今天才注意到,如果今天是 12 月而上周一是 11 月,则它不起作用。
I'm a total JS novice, is there another way to get these dates?
我是一个完全的 JS 新手,还有其他方法可以获取这些日期吗?
采纳答案by hereandnow78
if you dont want to do it with an external library you should work with timestamps. i created a solution where you would substract 60*60*24*7*1000 (which is 604800000, which is 1 week in milliseconds) from the current Date and go from there:
如果你不想用外部库来做,你应该使用时间戳。我创建了一个解决方案,您可以从当前日期中减去 60*60*24*7*1000(即 604800000,以毫秒为单位),然后从那里开始:
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));
回答by RobG
You can get the previous Monday by getting the Monday of this week and subtracting 7 days. The Sunday will be one day before that, so:
您可以通过获取本周的星期一并减去 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);
For 2012-12-03 I get:
对于 2012-12-03,我得到:
Mon 26 Nov 2012
Sun 25 Nov 2012
Is that what you want?
那是你要的吗?
// Or new date for the following Sunday
var sunday = new Date(d.getFullYear(), d.getMonth(), d.getDate() + 6);
which gives
这使
Sun 02 Dec 2012
In general, you can manipulate date objects by add and subtracting years, months and days. The object will handle negative values automatically, e.g.
通常,您可以通过添加和减去年、月和日来操作日期对象。该对象将自动处理负值,例如
var d = new Date(2012,11,0)
Will create a date for 2012-11-30 (noting that months are zero based so 11 is December). Also:
将为 2012-11-30 创建一个日期(注意月份是从零开始的,所以 11 是 12 月)。还:
d.setMonth(d.getMonth() - 1); // 2012-10-30
d.setDate(d.getDate() - 30); // 2012-09-30
回答by Toni Toni Chopper
You could use a library like moment.js.
See the subtractmethod http://momentjs.com/docs/#/manipulating/subtract/
你可以使用像 moment.js 这样的库。见subtract方法http://momentjs.com/docs/#/manipulating/subtract/
回答by Alex Lapa
A few answers mentioned moment, but no one wrote about this simple method:
一些答案提到了时刻,但没有人写过这个简单的方法:
moment().day(-13) // Monday last week
moment().day(-7) // Sunday last week
.daysets a week day, so it doesn't matter what day is it today, only week matters.
.day设置一个工作日,所以今天是什么日子并不重要,只有周很重要。
回答by friendly
it can be this simple.
可以这么简单。
var today = new Date();
var sunday = new Date(this.today.getFullYear(), this.today.getMonth(), this.today.getDate() - this.today.getDay());
回答by Alfredo Perez
Using Moment you can do the following
使用 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 );
回答by Aman Jain
This is the general solution of find any day of any week.
这是 find 任何一周的任何一天的一般解决方案。
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
回答by urpi5
Here you have a multi-purpose function:
在这里你有一个多用途的功能:
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;
}
You can find the "clone(obj)" function in the next post: https://stackoverflow.com/a/728694/6751764
您可以在下一篇文章中找到“clone(obj)”函数:https: //stackoverflow.com/a/728694/6751764

