getDay() vs getUTCDay()

When I apply the below code then it gives me the correct output but when I use getUTCDay() then it gives me the incorrect output.
Why?

for (var year = 2014; year <= 2050; year++) {

var d = new Date(year, 0, 1);

if (d.getDay() === 0) {

    console.log("1st Jan is being a Sunday" + year);

}

}

I suspect the issue might not be getDate vs getUTCDate, but how the date is created. Not entirely sure, but i might try logging out the full date and utc date for each in your console.log

The idea came from this: node.js - Why I canot get the same result using the getDay() method and the getUTCDay() method on the Javascript Date object - Stack Overflow

Hi, I cannot replicate the issue. I tested it with the following code:

let same=0;
for (var year = 2014; year <= 2044; year++) {
  var d = new Date(year, 0, 1);
  
  if (d.getDay()===d.getUTCDay())
    console.log(++same,'true');

  if (d.getDay() === 0) 
    console.log("1st Jan is being a Sunday" + year);

  if (d.getUTCDay() === 0) 
    console.log("UTC 1st Jan is being a Sunday" + year);
}

every year getDay() and getUTCDay() returns the same output.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.