简体   繁体   English

从星期天开始获取星期几

[英]Get week number with week starting from sunday

I currently have this code where I get the week number alongside the start/end day of that week with add and substract buttons: 我目前有以下代码,在该代码中,我可以通过添加和减号按钮获得星期几以及该周的开始/结束日期:

 Date.prototype.getWeekNumber = function() { var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate())); var dayNum = d.getUTCDay() || 7; d.setUTCDate(d.getUTCDate() + 4 - dayNum); var yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1)); return Math.ceil((((d - yearStart) / 86400000) + 1) / 7); }; var curr; weekReset(); function display() { start.textContent = curr; end.textContent = endOfWeek(curr); week.textContent = ("Week " + curr.getWeekNumber()); } function weekReset() { curr = startOfWeek(new Date()); display(); } function startOfWeek(date) { var start = new Date(date); start.setHours(0, 0, 0, 0); start.setDate(start.getDate() - start.getDay()); return start; } function endOfWeek(date) { date = startOfWeek(date); date.setDate(date.getDate() + 6); return date; } function weekPlus(weeks) { curr.setDate(curr.getDate() + 7 * weeks); display(); } 
 <div id="start">start</div> <div id="end">end</div> <div id="week">week</div> <button onclick="weekReset()">current</button> <button onclick="weekPlus(1)">add</button> <button onclick="weekPlus(-1)">substract</button> 

The problem is that it show's the current week number as 34 ; 问题是它显示当前星期数为34

starting on Sun Aug 27 8月27日星期日开始

ending on Sat Sep 02 9月02星期六结束

... when this should be the week number 35 . ...这应该是第35周。

So how can I change this function to count sunday as the first day of the week and saturday as the last day of the week? 那么,如何更改此功能以将星期天计算为一周的第一天,将星期六计算为一周的最后一天?

Any help is appreciated. 任何帮助表示赞赏。

Your code d.setUTCDate(d.getUTCDate() + 4 - dayNum); 您的代码d.setUTCDate(d.getUTCDate() + 4 - dayNum); is setting your date back to three days earlier. 正在将您的日期重新设置为三天前。 I think that's the reason you are getting previous week number. 我认为这就是您获得上周人数的原因。 I checked removing the code and it worked fine. 我检查了删除代码,它工作正常。

Your issue is here: 您的问题在这里:

var dayNum = d.getUTCDay() || 7;
d.setUTCDate(d.getUTCDate() + 4 - dayNum);

That code is for ISO weeks and adjusts Sunday's day number to 7 and sets the date to Thursday, where the week is Monday to Sunday. 该代码用于ISO周,并将周日的天数调整为7,并将日期设置为周四,其中周是周一至周日。

You want weeks Sunday to Saturday and want the week of the Sunday, so use: 您想要星期天到星期六有几个星期,而又想星期几是星期几,所以使用:

var dayNum = d.getUTCDay();
d.setUTCDate(d.getUTCDate() - dayNum);

which can be reduced to: 可以简化为:

d.setUTCDate(d.getUTCDate() - d.getUTCDay());

  Date.prototype.getWeekNumber = function () { var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate())); d.setUTCDate(d.getUTCDate() - d.getUTCDay()); var yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1)); return Math.ceil((((d - yearStart) / 86400000) + 1) / 7); }; var c = new Date(2017,7,26); console.log(c.getWeekNumber() + ':' + c.toString()); var d = new Date(2017,7,27); console.log(d.getWeekNumber() + ':' + d.toString()); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM