JavaScript – 从当前date开始获取一周中的第一天
我需要最快的方法来获得一周的第一天。 例如,今天是11月11日星期四,我需要把这个date转换成11月8日(星期一)。 我需要MongoDB地图function最快的方法,有什么想法?
使用Date对象的getDay
方法,您可以知道星期几(0 =星期日,1 =星期一等)。
然后可以减去这个天数加一,例如:
function getMonday(d) { d = new Date(d); var day = d.getDay(), diff = d.getDate() - day + (day == 0 ? -6:1); // adjust when day is sunday return new Date(d.setDate(diff)); } getMonday(new Date()); // Mon Nov 08 2010
不知道它是如何比较的性能,但这个工程。
var today = new Date(); var day = today.getDay() || 7; // Get current day number, converting Sun. to 7 if( day !== 1 ) // Only manipulate the date if it isn't Mon. today.setHours(-24 * (day - 1)); // Set the hours to day number minus 1 // multiplied by negative 24 alert(today); // will be Monday
或者作为一个function:
function getMonday( date ) { var day = date.getDay() || 7; if( day !== 1 ) date.setHours(-24 * (day - 1)); return date; } getMonday(new Date());
查看Date.js
Date.today().previous().monday()
我正在使用这个
function get_next_week_start() { var now = new Date(); var next_week_start = new Date(now.getFullYear(), now.getMonth(), now.getDate()+(8 - now.getDay())); return next_week_start; }
退房: moment.js
例:
moment().day(-7); // last Sunday (0 - 7) moment().day(7); // next Sunday (0 + 7) moment().day(10); // next Wednesday (3 + 7) moment().day(24); // 3 Wednesdays from now (3 + 7 + 7 + 7)
奖金:也可以和node.js一起使用
此函数使用当前毫秒时间来减去当前星期,然后如果当天date是星期一,则再减去一星期(JavaScript从星期日算起)。
function getMonday(fromDate) { // length of one day i milliseconds var dayLength = 24 * 60 * 60 * 1000; // Get the current date (without time) var currentDate = new Date(fromDate.getFullYear(), fromDate.getMonth(), fromDate.getDate()); // Get the current date's millisecond for this week var currentWeekDayMillisecond = ((currentDate.getDay()) * dayLength); // subtract the current date with the current date's millisecond for this week var monday = new Date(currentDate.getTime() - currentWeekDayMillisecond + dayLength); if (monday > currentDate) { // It is sunday, so we need to go back further monday = new Date(monday.getTime() - (dayLength * 7)); } return monday; }
我从一个月到另一个(也是几年)testing了一下,似乎正常工作。
晚上好,
我更喜欢只是一个简单的扩展方法:
Date.prototype.startOfWeek = function (pStartOfWeek) { var mDifference = this.getDay() - pStartOfWeek; if (mDifference < 0) { mDifference += 7; } return new Date(this.addDays(mDifference * -1)); }
你会注意到这实际上使用了另一个我使用的扩展方法:
Date.prototype.addDays = function (pDays) { var mDate = new Date(this.valueOf()); mDate.setDate(mDate.getDate() + pDays); return mDate; };
现在,如果你的星期天从星期天开始,为pStartOfWeekparameter passing一个“0”,如下所示:
var mThisSunday = new Date().startOfWeek(0);
同样,如果星期一从星期几开始,则为pStartOfWeekparameter passing一个“1”:
var mThisMonday = new Date().startOfWeek(1);
问候,
setDate()与上面注释中提到的月边界有关。 一个干净的解决方法是使用纪元时间戳,而不是date对象(令人惊讶的违反直觉)方法finddate差异。 即
function getPreviousMonday(fromDate) { var dayMillisecs = 24 * 60 * 60 * 1000; // Get Date object truncated to date. var d = new Date(new Date(fromDate || Date()).toISOString().slice(0, 10)); // If today is Sunday (day 0) subtract an extra 7 days. var dayDiff = d.getDay() === 0 ? 7 : 0; // Get date diff in millisecs to avoid setDate() bugs with month boundaries. var mondayMillisecs = d.getTime() - (d.getDay() + dayDiff) * dayMillisecs; // Return date as YYYY-MM-DD string. return new Date(mondayMillisecs).toISOString().slice(0, 10); }
这是我的解决scheme:
function getWeekDates(){ var day_milliseconds = 24*60*60*1000; var dates = []; var current_date = new Date(); var monday = new Date(current_date.getTime()-(current_date.getDay()-1)*day_milliseconds); var sunday = new Date(monday.getTime()+6*day_milliseconds); dates.push(monday); for(var i = 1; i < 6; i++){ dates.push(new Date(monday.getTime()+i*day_milliseconds)); } dates.push(sunday); return dates; }
现在你可以通过返回的数组索引来selectdate。
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(weekStartDate.getDate()+6));
这将工作得很好。