INs*_*INs 140 javascript algorithm
我需要以最快的方式获得一周的第一天.例如:今天是11月11日,星期四; 我想要本周的第一天,也就是11月8日和星期一.我需要MongoDB地图功能最快的方法,任何想法?
CMS*_*CMS 279
使用getDayDate对象的方法,您可以知道星期几的数量(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
Run Code Online (Sandbox Code Playgroud)
use*_*716 49
不确定它如何比较性能,但这是有效的.
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
Run Code Online (Sandbox Code Playgroud)
或者作为一个功能:
function getMonday( date ) {
var day = date.getDay() || 7;
if( day !== 1 )
date.setHours(-24 * (day - 1));
return date;
}
getMonday(new Date());
Run Code Online (Sandbox Code Playgroud)
要从今天开始获取一周的第一天的日期,您可以使用以下方法:
function getUpcomingSunday() {
const date = new Date();
const today = date.getDate();
const dayOfTheWeek = date.getDay();
const newDate = date.setDate(today - dayOfTheWeek + 7);
return new Date(newDate);
}
console.log(getUpcomingSunday());Run Code Online (Sandbox Code Playgroud)
或者从今天开始获取一周的最后一天:
function getLastSunday() {
const date = new Date();
const today = date.getDate();
const dayOfTheWeek = date.getDay();
const newDate = date.setDate(today - (dayOfTheWeek || 7));
return new Date(newDate);
}
console.log(getLastSunday());Run Code Online (Sandbox Code Playgroud)
* 根据您的时区,一周的开始不必从星期日开始,它可以从星期五、星期六、星期一或您的机器设置的任何其他日期开始。这些方法将说明这一点。
* 您也可以使用如下toISOString方法对其进行格式化:getLastSunday().toISOString()
小智 6
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));
Run Code Online (Sandbox Code Playgroud)
这样会很好用。
CMS的答案是正确的,但假设星期一是一周的第一天。
钱德勒·兹沃勒(Chandler Zwolle)的回答是正确的,但摆弄了日期原型。
其他以小时/分钟/秒/毫秒为单位的答案是错误的。
下面的函数是正确的,并且将日期作为第一个参数,并将所需的一周的第一天作为第二个参数(0代表星期日,1代表星期一,依此类推)。注意:小时,分钟和秒设置为0以表示一天的开始。
function firstDayOfWeek(dateObject, firstDayOfWeekIndex) {
const dayOfWeek = dateObject.getDay(),
firstDayOfWeek = new Date(dateObject),
diff = dayOfWeek >= firstDayOfWeekIndex ?
dayOfWeek - firstDayOfWeekIndex :
6 - dayOfWeek
firstDayOfWeek.setDate(dateObject.getDate() - diff)
firstDayOfWeek.setHours(0,0,0,0)
return firstDayOfWeek
}
// August 18th was a Saturday
let lastMonday = firstDayOfWeek(new Date('August 18, 2018 03:24:00'), 1)
// outputs something like "Mon Aug 13 2018 00:00:00 GMT+0200"
// (may vary according to your time zone)
document.write(lastMonday)Run Code Online (Sandbox Code Playgroud)
我正在用这个
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;
}
Run Code Online (Sandbox Code Playgroud)
返回周一上午 00 点至周一上午 00 点。
const now = new Date()
const startOfWeek = new Date(now.getFullYear(), now.getMonth(), now.getDate() - now.getDay() + 1)
const endOfWeek = new Date(now.getFullYear(), now.getMonth(), startOfWeek.getDate() + 7)
Run Code Online (Sandbox Code Playgroud)