我需要最快的方法得到一周的第一天。例如:今天是11月11日,是星期四;我想要这周的第一天,也就是11月8日,一个星期一。我需要MongoDB映射函数的最快方法,有什么想法吗?
当前回答
一个只有数学计算的例子,没有任何Date函数。
const date = new date (); Const ts = +日期; const mondayTS = ts % (60 * 60 * 24 * (7-4) * 1000); const monday =新的日期(星期一); console.log(monday.toISOString(), 'Day:', monday.getDay());
const formatTS = v => new Date(v).toISOString(); const adjust = (v, d = 1) => v - v % (d * 1000); const d = new Date('2020-04-22T21:48:17.468Z'); const ts = +d; // 1587592097468 const test = v => console.log(formatTS(adjust(ts, v))); test(); // 2020-04-22T21:48:17.000Z test(60); // 2020-04-22T21:48:00.000Z test(60 * 60); // 2020-04-22T21:00:00.000Z test(60 * 60 * 24); // 2020-04-22T00:00:00.000Z test(60 * 60 * 24 * (7-4)); // 2020-04-20T00:00:00.000Z, monday // So, what does `(7-4)` mean? // 7 - days number in the week // 4 - shifting for the weekday number of the first second of the 1970 year, the first time stamp second. // new Date(0) ---> 1970-01-01T00:00:00.000Z // new Date(0).getDay() ---> 4
其他回答
以下是我的解决方案:
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;
}
现在你可以通过返回的数组索引来选择日期。
该函数使用当前毫秒时间减去当前周,如果当前日期是周一,则再减去一周(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;
}
当一周从一个月延伸到另一个月(也包括几年)时,我对它进行了测试,它似乎可以正常工作。
我用这个:
let current_date = new Date();
let days_to_monday = 1 - current_date.getDay();
monday_date = current_date.addDays(days_to_monday);
// https://stackoverflow.com/a/563442/6533037
Date.prototype.addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
}
它工作得很好。
晚上好,
我更喜欢有一个简单的扩展方法:
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;
};
现在,如果你的周从周日开始,为pStartOfWeek参数传递一个“0”,如下所示:
var mThisSunday = new Date().startOfWeek(0);
类似地,如果你的周从星期一开始,为pStartOfWeek参数传递一个“1”:
var mThisMonday = new Date().startOfWeek(1);
问候,
我在用这个
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;
}
推荐文章
- 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输入的清除?