如何使用javascript获取特定年份/月份的周数?
例如: 2013年11月的周数为45到49如何使用javascript获取特定年份/月份的周数?,javascript,cordova,calendar,Javascript,Cordova,Calendar,例如: 2013年11月的周数为45到49 2014年12月的周数为49至53这是德国/欧洲/ISO周的解决方案 查找给定日期所在周的星期四。星期四确定请求的星期所属的年份 查找当年1月4日一周中的星期四。本周四是第一周 计算本周的星期四和第一周的星期四加一之间的周差。。。这将是给定日期的周数 试试这个 function thursday(mydate) { var Th=new Date(); Th.setTime(mydate.getTime() + (3-((mydate.getD
2014年12月的周数为49至53这是德国/欧洲/ISO周的解决方案
function thursday(mydate) {
var Th=new Date();
Th.setTime(mydate.getTime() + (3-((mydate.getDay()+6) % 7)) * 86400000);
return Th;
}
function getCalWeek(y, m, d) {
thedate=new Date(y, m-1, d);
ThursDate=thursday(thedate);
weekYear=ThursDate.getFullYear();
ThursWeek1=thursday(new Date(weekYear,0,4));
theweek=Math.floor(1.5+(ThursDate.getTime()-ThursWeek1.getTime())/86400000/7);
return theweek;
}
console.log("The week of the given date is: " + getCalWeek(2013, 11, 5));
编辑:对于您的特定问题,您需要给出月份和年份,然后计算周数
function daysInMonth(month,year) {
var m = [31,28,31,30,31,30,31,31,30,31,30,31];
if (month != 2) return m[month - 1];
if (year%4 != 0) return m[1];
if (year%100 == 0 && year%400 != 0) return m[1];
return m[1] + 1;
}
function getWeekRange(m, y) {
var startWeek = getCalWeek(y, m, 1);
var endWeek = getCalWeek(y, m, daysInMonth(m, y));
return startWeek + " to " + endWeek;
}
世界各地都不一样。例如,在德国,第1周是包含1月4日的一周(从周一开始到周日结束)。你也可以说:第1周是第一周(从周一开始),至少1月4日,这可能是重复的,这在网络搜索中并不难研究