0

Hi I'm trying to change this calendar's starting weekday from Sunday to Monday. But I don't understand what's going on in the var k = ... line. What is this line of code trying to say in human language? https://codepen.io/xmark/pen/WQaXdv?editors=1010

// June 2018
var k = lastDay_of_LastMonth - firstDay_of_Month+1;
27


//_____________________________________________________________________
// TEST

var lastDay_of_LastMonth = new Date(2018, 5, 0); // May 31st 2018
document.write('Today is: ' + lastDay_of_LastMonth.toLocaleString());
// Today is: 5/31/2018, 12:00:00 AM

var firstDay_of_Month = new Date(2018, 5, 1); // June 1st 2018
firstDay_of_Month."getDay()";
5

// June 2018
var k = lastDay_of_LastMonth - firstDay_of_Month+1;

lastDay_of_LastMonth.setDate(lastDay_of_LastMonth.getDate() - 6);
document.write('<br>X days ago was: ' + lastDay_of_LastMonth.toLocaleString());
// X days ago was: 5/25/2018, 12:00:00 AM

//_____________________________________________________________________

// June 2018
27 = 31 - 5+1;

I see from this thread that this code is calculating (Date - Days), counting days backwards. But I can't understand what it is doing in human language for the calendar code. Shouldn't the math 31-(5+1) = 25 meaning it's going 6 days back in time why do I get the value 27 back? Subtract days from a date in JavaScript

piggy
  • 83
  • 1
  • 3
  • 11
  • I fixed some of the comments they were confusing things. – piggy Jul 03 '18 at 01:11
  • It's much better if you post your code here as a runnable snippet, but please see [*How to create a minimal, complete and verifiable example*](https://stackoverflow.com/help/mcve) first. ;-) – RobG Jul 03 '18 at 01:14
  • I updated a previous thread with my newly edited code files. https://stackoverflow.com/questions/51014363/how-to-change-calendars-start-date-layout-from-sunday-to-monday – piggy Jul 03 '18 at 01:23

2 Answers2

2

From the codepen that you attached, the developer is using k to get the days outside of the current month that should come up in the calendar (i.e. if our month is starting on Tuesday then Sunday & Monday are in the previous month, but we need to get the "day of the month numbers" of these days), now they are using k in two occasions:

1: to get the days from the previous month var k = lastDayOfLastMonth - firstDayOfMonth+1;

2: to get the days from the next month:

else if ( i == lastDateOfMonth ) {
  var k=1;
  for(dow; dow < 6; dow++) {
    html += '<td class="not-current">' + k + '</td>';
    k++;
  }
}

In the first occasion, they are calculating the difference between the last day of the last month and the first day of this month, the +1 here is because you're working with indexes.

In the second occasion, it's safe to say that every month has 1-6, so they loop up to 6 to fill up the last week in the calendar.

Naguib Ihab
  • 4,259
  • 7
  • 44
  • 80
  • Thanks for clarifying the before and after parts I was blind to it until now. Could you take a quick look at my current code and point out which section I need to dig deeper on? Because my calendar breaks whenever the **1st date of a month is on a Sunday.** https://stackoverflow.com/questions/51014363/how-to-change-calendars-start-date-layout-from-sunday-to-monday – piggy Jul 03 '18 at 01:49
  • 1
    @Abby I answered your other question :) – Naguib Ihab Jul 03 '18 at 02:35
1

Order of operations in this language gives equal precedence to the addition and subtraction operators.

In other words, it uses PEMDAS.

So it'll do 31-5: 26

Then 26 + 1: 27

CSharpFiasco
  • 204
  • 3
  • 8