Menu Close

The Hidden Complexities of the getDay Method in JavaScript

In JavaScript, we often work with Date objects to represent specific points in time. In this case, I am trying to get the day of the week for a given date represented as a string in the “YYYY-MM-DD” format.

First :

The getDay method of the Date object in JavaScript returns the day of the week for a given date, as a number from 0 (Sunday) to 6 (Saturday). It does not return the day of the month.

If you want to get the day of the month for a given date, you should use the getDate method instead. This method returns the day of the month for the Date object, as a number from 1 to 31.

Initially, I had the following function to get week day:

function getWeekDay(dateString) {
    const date = new Date(dateString);
    const dayOfWeek = date.getDay();
    const weekDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
    return weekDays[dayOfWeek];
}

During my testing, I noticed that the function always offsets the day by 1. After some research, I discovered that ECMAScript Date objects are based on a time value that is an offset from 1970-01-01T00:00:00Z, making them inherently UTC. This means that for a given time value, the value returned by the getDay the method may be different depending on the host timezone offset.

To fix this issue, I need to specify the timezone offset when creating the Date object, using the new Date constructor’s optional fourth argument. This argument represents the number of minutes the timezone is offset from UTC. For example, to create a Date object representing the date “January 1st, 2022” in the timezone that is 2 hours behind UTC (120 minutes), I would use the following code:

const date = new Date(2022, 0, 1, 0, -120);

By specifying the timezone offset, I can ensure that the Date object is created with the correct time value for the desired timezone. I can then use the getDay method to retrieve the day of the week, and it should return the correct value.”

“It is not ideal to use the approach described above, since it requires finding the timezone offset in order to create the Date object with the correct time value. An alternative approach is to use the new Date constructor without specifying the timezone offset, like so:

const date = new Date(year, month, day);

Then, use the getDay method on the new Date object to get the day of the week. This approach has the advantage of not requiring knowledge of the timezone offset, and it is generally easier to understand and maintain.

In JavaScript, the month argument for the new Date constructor is zero-indexed, meaning that it is represented by a number from 0 to 11, with 0 representing January, 1 representing February, and so on. This can be a source of confusion for developers who are not familiar with this convention, as it is not immediately obvious that the month’s argument should be decremented by one.

For example, to create a Date object representing the date “January 1st, 2022”, you would use the following code:

const date = new Date(2022, 0, 1);

Thus, the final version of the code looks like this:

function getWeekDay(dateString) {
    // dateString is sanitized yyyy-mm-dd format string
    const parts = dateString.split("-");
    const year = parseInt(parts[0], 10);
    const month = parseInt(parts[1], 10) - 1;
    const day = parseInt(parts[2], 10);

    const date = new Date(year, month, day);

    const dayOfWeek = date.getDay();
    const weekDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
    return weekDays[dayOfWeek];
}

Leave a Reply

Your email address will not be published. Required fields are marked *