[英]How to change calendar's start date layout from Sunday to Monday?
Hi I'm trying to learn JavaScript from this calendar example code by the author xMark. 嗨,我正在尝试从作者xMark的日历示例代码中学习JavaScript。 But his example uses Sunday as the first day of the week.
但他的示例使用星期日作为一周的第一天。 I want it to show Monday as the first day of the week.
我希望它将星期一显示为一周的第一天。 So far I have only managed to change the header labels but not the dates' correct positions.
到目前为止,我仅设法更改标题标签,而没有更改日期的正确位置。 https://codepen.io/xmark/pen/WQaXdv
https://codepen.io/xmark/pen/WQaXdv
So I've been trying to understand where in his code I can make the dates shift one step to the left. 因此,我一直试图了解在他的代码中我可以使日期向左移动一步。 But I can't figure out where in the code this shift should occur.
但是我不知道这种变化应该在代码中的什么地方发生。
The section where I'm mostly stuck is in Calendar.prototype.showMonth = function(y, m) {...}
where I think is the part that requires changing. 我最受困的部分在
Calendar.prototype.showMonth = function(y, m) {...}
,我认为这是需要更改的部分。 In this function I don't understand why the author var k = lastDay_of_LastMonth - firstDay_of_Month+1;
在此函数中,我无法理解为什么作者
var k = lastDay_of_LastMonth - firstDay_of_Month+1;
adds +1 to the last variable? 将+1添加到最后一个变量?
After var k = ...
I'm completely lost. 在
var k = ...
我完全迷路了。 Whenever I try to change things the whole calendar just breaks apart. 每当我尝试更改事物时,整个日历就会崩溃。
@Naguib @纳吉布
var d7 = new Date(2018, 6, 1); d7 "Sun Jul 01 2018" 00:00:00 GMT+0200 (Central European Summer Time) var firstDay_of_Month = new Date(2018, 6, 1).getDay(); firstDay_of_Month 0 // July 1st is on a Sunday. //_________________________________________________________________ var d8 = new Date(2018, 7, 1); d8 "Wed Aug 01 2018" 00:00:00 GMT+0200 (Central European Summer Time) var firstDay_of_Month = new Date(2018, 7, 1).getDay(); firstDay_of_Month 3 // August 1st is on a Wednesday. //_________________________________________________________________ var dNaguib = new Date(2018, 6, 7); dNaguib "Sat Jul 07 2018" 00:00:00 GMT+0200 (Central European Summer Time) var dNaguib = new Date(2018, 6, 7).getDay(); dNaguib 6 // July 7th is on a Saturday. // Why then does this dynamic code looking for the 7th each month // make everything in the calendar work? var firstDay_of_Month = new Date(y, m, 7).getDay(); // Naguib // While the original code looking for the 1st each month make // some months break the calendar? var firstDay_of_Month = new Date(y, m, 1).getDay(); // Original
Check out this code from Nikhil Talreja. 查看Nikhil Talreja的这段代码 。 It should give you a good idea as to how to get the calendar working with Monday as a start date.
它应该使您对如何使日历以星期一作为开始日期有一个好主意。 A lso, check out the similar question.
同样,请检查类似的问题。
Essentially, he uses some for loops and labels, such that: 本质上,他使用一些for循环和标签,例如:
cal_days_labels = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat','Sun'];
and 和
for (var j = 1; j <= 7; j++)
The project is similar to yours I would imagine, so hopefully this helps. 该项目与我想像的项目相似,因此希望能有所帮助。
I went through the codepen you attached and went through every part in the code where Sunday is mentioned and modified it to become this: https://codepen.io/naguibihab/pen/aKMYpB 我仔细阅读了您所附的Codepen,并仔细阅读了提到星期日的代码中的每个部分,并将其修改为: https : //codepen.io/naguibihab/pen/aKMYpB
Here are the changes that I did: https://github.com/naguibihab/js-goodies/commit/aa14d87eea68860650aee3383b458a0b00fb64a9 这是我所做的更改: https : //github.com/naguibihab/js-goodies/commit/aa14d87eea68860650aee3383b458a0b00fb64a9
I'll explain why I did each change, this is taking me back to my college days where I have to explain my code so bear with me: 我将解释为什么要进行每次更改,这使我回到了大学时代,在这里我必须解释我的代码,所以请多多包涵:
1: 1:
this.DaysOfWeek = [
'Mon',
'Tue',
'Wed',
'Thu',
'Fri',
'Sat',
'Sun'
];
That's the header row's title in the calendar, that's simply text no logic changes there 那是日历中标题行的标题,只是文本,没有逻辑更改
2: firstDayOfMonth = 6
Your first day of the month is now Monday and not Sunday, you can also write , firstDayOfMonth = new Date(y, m, 7).getDay()
to get the same result but I think the first one makes it a bit clearer to the reader as we'll always be getting 6
anyway 2:
firstDayOfMonth = 6
您本月的第一天现在是星期一,而不是星期日,您还可以编写, firstDayOfMonth = new Date(y, m, 7).getDay()
得到相同的结果,但是我认为第一个对读者来说更清楚了,因为我们总会得到6
3: if ( dow == 1 ) {
start a new row on Monday instead of Sunday 3:
if ( dow == 1 ) {
在星期一而不是星期日开始新的一行
4: if ( dow == 0 ) {
close the row on Sunday instead of Saturday 4:
if ( dow == 0 ) {
在星期日而不是星期六关闭该行
5: for(dow; dow < 7; dow++) {
next month's "not-current" numbers can go up to Sunday so we need an extra iteration there (There might be a better way to do that without increasing iterations but I'm too lazy to figure it out now) 5:
for(dow; dow < 7; dow++) {
下个月的“非当前”数字可能会升至星期日,因此我们需要在那里进行额外的迭代(也许有更好的方法可以不增加迭代次数,但是我太懒了现在不知道了)
It mostly is a concept of "try to change something and see what happens" kind of method to get there, so what I did was go over each area where I suspect Sunday is affecting the code and tried to change it to see what happens. 它主要是一种“尝试更改某些内容并查看发生了什么”的概念,以便到达那里,因此我所做的工作是遍历我怀疑星期日影响代码的每个区域,然后尝试进行更改以查看发生了什么。
Let me know in the comments if that doesn't make enough sense. 在评论中让我知道这是否没有足够的道理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.