我需要最快的方法得到一周的第一天。例如:今天是11月11日,是星期四;我想要这周的第一天,也就是11月8日,一个星期一。我需要MongoDB映射函数的最快方法,有什么想法吗?
当前回答
区分本地时间和UTC时间是很重要的。我想用UTC找到一周的开始,所以我使用了下面的函数。
function start_of_week_utc(date, start_day = 1) {
// Returns the start of the week containing a 'date'. Monday 00:00 UTC is
// considered to be the boundary between adjacent weeks, unless 'start_day' is
// specified. A Date object is returned.
date = new Date(date);
const day_of_month = date.getUTCDate();
const day_of_week = date.getUTCDay();
const difference_in_days = (
day_of_week >= start_day
? day_of_week - start_day
: day_of_week - start_day + 7
);
date.setUTCDate(day_of_month - difference_in_days);
date.setUTCHours(0);
date.setUTCMinutes(0);
date.setUTCSeconds(0);
date.setUTCMilliseconds(0);
return date;
}
要在给定时区中找到一周的开始,首先将时区偏移量添加到输入日期,然后从输出日期中减去时区偏移量。
const local_start_of_week = new Date(
start_of_week_utc(
date.getTime() + timezone_offset_ms
).getTime() - timezone_offset_ms
);
其他回答
使用Date对象的getDay方法,您可以知道一周中的天数(0=星期日,1=星期一,等等)。
然后你可以用这个天数加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
区分本地时间和UTC时间是很重要的。我想用UTC找到一周的开始,所以我使用了下面的函数。
function start_of_week_utc(date, start_day = 1) {
// Returns the start of the week containing a 'date'. Monday 00:00 UTC is
// considered to be the boundary between adjacent weeks, unless 'start_day' is
// specified. A Date object is returned.
date = new Date(date);
const day_of_month = date.getUTCDate();
const day_of_week = date.getUTCDay();
const difference_in_days = (
day_of_week >= start_day
? day_of_week - start_day
: day_of_week - start_day + 7
);
date.setUTCDate(day_of_month - difference_in_days);
date.setUTCHours(0);
date.setUTCMinutes(0);
date.setUTCSeconds(0);
date.setUTCMilliseconds(0);
return date;
}
要在给定时区中找到一周的开始,首先将时区偏移量添加到输入日期,然后从输出日期中减去时区偏移量。
const local_start_of_week = new Date(
start_of_week_utc(
date.getTime() + timezone_offset_ms
).getTime() - timezone_offset_ms
);
CMS的答案是正确的,但假设星期一是一周的第一天。 钱德勒·兹沃勒的答案是正确的,但摆弄了日期原型。 其他加/减小时/分钟/秒/毫秒的答案是错误的,因为不是所有的日子都有24小时。
下面的函数是正确的,它将日期作为第一个参数,将所需的一周第一天作为第二个参数(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)
该函数使用当前毫秒时间减去当前周,如果当前日期是周一,则再减去一周(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;
}
当一周从一个月延伸到另一个月(也包括几年)时,我对它进行了测试,它似乎可以正常工作。
简单的解决办法,得到一周的第一天。
使用这种解决方案,可以设置任意的星期开始(例如,星期日= 0,星期一= 1,星期二= 2,等等)。
function getBeginOfWeek(date = new Date(), startOfWeek = 1) {
const result = new Date(date);
while (result.getDay() !== startOfWeek) {
result.setDate(result.getDate() - 1);
}
return result;
}
解决方案正确地按月包装(由于使用了Date.setDate()) 对于startOfWeek,可以使用与Date.getDay()中相同的常量
推荐文章
- JSHint和jQuery: '$'没有定义
- 模仿JavaScript中的集合?
- 用JavaScript验证电话号码
- 如何在HTML5中改变视频的播放速度?
- 谷歌地图API v3:我可以setZoom后fitBounds?
- 找出质数最快的算法是什么?
- ES6/2015中的null安全属性访问(和条件赋值)
- 与push()相反;
- JS字符串“+”vs concat方法
- AngularJS使用ng-class切换类
- 访问Handlebars.js每次循环范围之外的变量
- 如何用JavaScript截屏一个div ?
- 如何为其他域设置cookie
- 如何减去日期/时间在JavaScript?
- 如何检测“搜索”HTML5输入的清除?