如何在JavaScript中获取本周的第一天和最后一天
我today = new Date();
目的。 我需要得到本周的第一天和最后一天。 周日和周一我需要两个变种作为一周的开始和结束date。 我现在有点混淆与代码。 你能帮我吗?
var curr = new Date; // get current date var first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week var last = first + 6; // last day is the first day + 6 var firstday = new Date(curr.setDate(first)).toUTCString(); var lastday = new Date(curr.setDate(last)).toUTCString(); firstday "Sun, 06 Mar 2011 12:25:40 GMT" lastday "Sat, 12 Mar 2011 12:25:40 GMT"
这适用于第一天=本周的星期天,最后一天=本周的星期六。 把它延长到周一到周日是微不足道的。
让它在不同月份的第一天和最后一天工作,留给用户练习
小心接受的答案,它不会设置时间00:00:00和23:59:59,所以你可以有问题。
我build议使用Moment.js来处理date。 对于你的情况:
var startOfWeek = moment().startOf('week').toDate(); var endOfWeek = moment().endOf('week').toDate();
这只是一个小用例,执行大量复杂操作非常简单。
你可以在这里看到更多的信息: http : //momentjs.com/
您还可以使用以下代码行来获取本周的第一个和最后一个date:
var curr = new Date; var firstday = new Date(curr.setDate(curr.getDate() - curr.getDay())); var lastday = new Date(curr.setDate(curr.getDate() - curr.getDay()+6));
希望它会有用..
这里有一个快速的方法来获取第一天和最后一天,任何一天的开始。 知道:
1天= 86,400,000毫秒。
JSdate值以毫秒为单位
配方:找出需要移除多less天才能获得本周的开始date(乘以1天的毫秒数)。 之后剩下的就是增加6天来获得结束的一天。
var startDay = 1; //0=sunday, 1=monday etc. var d = now.getDay(); //get the current day var weekStart = new Date(now.valueOf() - (d<=0 ? 7-startDay:d-startDay)*86400000); //rewind to start day var weekEnd = new Date(weekStart.valueOf() + 6*86400000); //add 6 days to get last day
我build议在这种情况下使用Moment.js。 我有情况下,我必须检查当前date时间,本周,本月和本季度date时间。 上面的答案帮助了我,所以我想分享其余的function。
只需以特定格式获取当前date时间即可
case 'Today': moment().format("DD/MM/YYYY h:mm A"); case 'This Week': moment().endOf('isoweek').format("DD/MM/YYYY h:mm A");
星期从星期天开始,星期六结束,如果我们简单地使用“星期”作为endOf函数的参数,但星期天作为一周结束,我们需要使用“isoweek”。
case 'This Month': moment().endOf('month').format("DD/MM/YYYY h:mm A"); case 'This Quarter': moment().endOf('quarter').format("DD/MM/YYYY h:mm A");
我根据自己的需要select了这种格式。 您可以根据您的要求更改格式。
krtek的方法有一些错误,我testing了这个
var startDay = 0; var weekStart = new Date(today.getDate() - (7 + today.getDay() - startDay) % 7); var weekEnd = new Date(today.getDate() + (6 - today.getDay() - startDay) % 7);
有用
你可以做这样的事情
var today = new Date(); var startDay = 0; var weekStart = new Date(today.getDate() - (7 + today.getDay() - startDay) % 7); var weekEnd = new Date(today.getDate() + (7 - today.getDay() - startDay) % 7);
startDay
是从0到6的数字,其中0代表星期天(即1 =星期一,2 =星期二等)。
SetDate
将设置月份的一天。 在月份的开始和结束时使用setDate
,将导致错误的一周
var curr = new Date("08-Jul-2014"); // get current date var first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week var last = first + 6; // last day is the first day + 6 var firstday = new Date(curr.setDate(first)); // 06-Jul-2014 var lastday = new Date(curr.setDate(last)); //12-Jul-2014
如果您设置date为2014年7月1日,则会显示第一天为2014年6月29日,而最后一天为2014年6月5日,而不是2014年7月5日
所以克服了我用过的这个问题
var curr = new Date(); day = curr.getDay(); firstday = new Date(curr.getTime() - 60*60*24* day*1000); //will return firstday (ie sunday) of the week lastday = new Date(curr.getTime() + 60 * 60 *24 * 6 * 1000); //adding (60*60*6*24*1000) means adding six days to the firstday which results in lastday (saturday) of the week
这工作跨越年份和月份的变化。
Date.prototype.GetFirstDayOfWeek = function() { return (new Date(this.setDate(this.getDate() - this.getDay()))); } Date.prototype.GetLastDayOfWeek = function() { return (new Date(this.setDate(this.getDate() - this.getDay() +6))); } var today = new Date(); alert(today.GetFirstDayOfWeek()); alert(today.GetLastDayOfWeek());
很好的build议,但你昨天有一个小问题。 您应该将其更改为:
lastday = new Date(firstday.getTime() + 60 * 60 *24 * 6 * 1000);
//get start of week; QT function _getStartOfWeek (date){ var iDayOfWeek = date.getDay(); var iDifference = date.getDate() - iDayOfWeek + (iDayOfWeek === 0 ? -6:1); return new Date(date.setDate(iDifference)); }, function _getEndOfWeek(date){ return new Date(date.setDate(date.getDate() + (7 - date.getDay()) === 7 ? 0 : (7 - date.getDay()) )); },
*当前date== 30.06.2016和星期一是一周中的第一天。
它也适用于不同的月份和年份。 经过qunit套件testing:
QUnit.module("Planung: Start of week"); QUnit.test("Should return start of week based on current date", function (assert) { var startOfWeek = Planung._getStartOfWeek(new Date()); assert.ok( startOfWeek , "returned date: "+ startOfWeek); }); QUnit.test("Should return start of week based on a sunday date", function (assert) { var startOfWeek = Planung._getStartOfWeek(new Date("2016-07-03")); assert.ok( startOfWeek , "returned date: "+ startOfWeek); }); QUnit.test("Should return start of week based on a monday date", function (assert) { var startOfWeek = Planung._getStartOfWeek(new Date("2016-06-27")); assert.ok( startOfWeek , "returned date: "+ startOfWeek); }); QUnit.module("Planung: End of week"); QUnit.test("Should return end of week based on current date", function (assert) { var endOfWeek = Planung._getEndOfWeek(new Date()); assert.ok( endOfWeek , "returned date: "+ endOfWeek); }); QUnit.test("Should return end of week based on sunday date with different month", function (assert) { var endOfWeek = Planung._getEndOfWeek(new Date("2016-07-03")); assert.ok( endOfWeek , "returned date: "+ endOfWeek); }); QUnit.test("Should return end of week based on monday date with different month", function (assert) { var endOfWeek = Planung._getEndOfWeek(new Date("2016-06-27")); assert.ok( endOfWeek , "returned date: "+ endOfWeek); }); QUnit.test("Should return end of week based on 01-06-2016 with different month", function (assert) { var endOfWeek = Planung._getEndOfWeek(new Date("2016-06-01")); assert.ok( endOfWeek , "returned date: "+ endOfWeek); }); QUnit.test("Should return end of week based on 21-06-2016 with different month", function (assert) { var endOfWeek = Planung._getEndOfWeek(new Date("2016-06-21")); assert.ok( endOfWeek , "returned date: "+ endOfWeek); }); QUnit.test("Should return end of week based on 28-12-2016 with different month and year", function (assert) { var endOfWeek = Planung._getEndOfWeek(new Date("2016-12-28")); assert.ok( endOfWeek , "returned date: "+ endOfWeek); }); QUnit.test("Should return end of week based on 01-01-2016 with different month and year", function (assert) { var endOfWeek = Planung._getEndOfWeek(new Date("2016-01-01")); assert.ok( endOfWeek , "returned date: "+ endOfWeek); });
虽然这个问题似乎过时了,但我必须指出一个问题。
问题: 2016年1月1日会发生什么?
我想大多数上述解决scheme计算一周的开始为27.12.2016。 基于这个原因,我认为,正确的计算应该像下面这样简单;
var d = new Date(), dayInMs = 1000 * 60 * 60 * 24, weekInMs = dayInMs * 7, startOfToday = new Date(d.getFullYear(), d.getMonth(), d.getDate()).valueOf(), todayElapsedTime = d.valueOf() - startOfToday, dayDiff = d.getDay() * dayInMs, dateDiff = dayDiff + todayElapsedTime, // finally startOfWeek = d.valueOf() - dateDiff, endOfWeek = startOfWeek + weekInMs - 1;
var dt = new Date() //current date of week var currentWeekDay = dt.getDay(); var lessDays = currentWeekDay == 0 ? 6 : currentWeekDay-1 var wkStart = new Date(new Date(dt).setDate(dt.getDate()- lessDays)); var wkEnd = new Date(new Date(wkStart).setDate(wkStart.getDate()+6));
这对于任何date场景都是有用的。
优秀的(和不可变的) date文件库处理这个最简洁:
const start = startOfWeek(date); const end = endOfWeek(date);
目前的方法为我所有的情况下工作(虽然我没有像年底,闰年testing的边界)。 在上面的代码中只有更正是参数是“isoWeek”,如果你想从星期一开始一周。
let startOfWeek = moment().startOf("isoWeek").toDate(); let endOfWeek = moment().endOf("isoWeek").toDate();
只要使用纯粹的JavaScript,您可以使用下面的function来获得一周的第一天和最后一天,并免费设置一周的开始date。
var weekday = []; weekday[0] = "Sunday"; weekday[1] = "Monday"; weekday[2] = "Tuesday"; weekday[3] = "Wednesday"; weekday[4] = "Thursday"; weekday[5] = "Friday"; weekday[6] = "Saturday"; function getFirstDayOfWeek(date, from) { //Default start week from 'Sunday'. You can change it yourself. from = from || 'Sunday'; var index = weekday.indexOf(from); var start = index >= 0 ? index : 0; var d = new Date(date); var day = d.getDay(); var diff = d.getDate() - day + (start > day ? start - 7 : start); d.setDate(diff); return d; };
星期的最后一天是星期几后的6天
function getLastDayOfWeek(date, from) { from = from || 'Sunday'; var index = weekday.indexOf(from); var start = index >= 0 ? index : 0; var d = new Date(date); var day = d.getDay(); var diff = d.getDate() - day + (start > day ? start - 1 : 6 + start); d.setDate(diff); return d; };
testing:
getFirstDayOfWeek('2017-10-16'); //--> Sun Oct 15 2017 getFirstDayOfWeek('2017-10-16', 'Monday'); //--> Mon Oct 16 2017 getFirstDayOfWeek('2017-10-16', 'Tuesday'); //--> Tue Oct 10 2017