JavaScript的 - 让周从当前日期的第一天 [英] JavaScript - get the first day of the week from current date
本文介绍了JavaScript的 - 让周从当前日期的第一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一周的第一天,最快的方式。比如今天是十一月的11和周四,我需要的日期转换到11日(星期一)8。我需要的最快方法MongoDB的地图功能,任何想法?
I need the fastest way to get the first day of the week. For example today is 11 of november and thursday, i need to convert that date to 8 of november (monday). I need the fastest method for MongoDB map function, any ideas?
推荐答案
使用Date对象的 getDay
方法,你就可以知道星期几的数量(即0 =星期日,1 =星期一等)。
Using the getDay
method of Date objects, you can know the number of day of the week (being 0=Sunday, 1=Monday, etc).
您可以再减去天数加一,例如:
You can then subtract that number of days plus one, for example:
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
这篇关于JavaScript的 - 让周从当前日期的第一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文