Testing the Javascript Data API | Task

Ole Ersoy
Jan - 02  -  1 min

Scenario

We are subtracting days from a date in Javascript like this:

const date = new Date('2010-10-10)
const behind = date.setDate(date.getDate()-8)

And we expect the new date to be 2010-10-02. However if we log the current day of the behind date like this, we get 1.

console.log(behind.getDate())

Approach

Instead of logging getDate() use getUTCDate():

console.log(behind.getUTCDate())

This will log the correct day.