Consider the following code snippet:
const d = new Date('2010-10-20');
console.log(d.getUTCDate());
If you run this, the console will output 20
.
However, if you modify the code like so:
const d = new Date('2010-10-20');
d.setHours(0, 0, 0, 0);
console.log(d.getUTCDate());
The output will now be 19
. Are you surprised by this result? Share your thoughts.
Check out this interactive demo on StackBlitz for more insights: StackBlitz Demo
Another interesting observation is that when you specify all zeroes for the time and then call getUTCDate()
, the expected date is logged:
const d3 = new Date('2000-10-20T00:00:00Z');
console.log(`D3: ${d3.getUTCDate()}`);