skip to Main Content

I am trying to generate a sequence of dates. However, when generating JS, it constantly misses October 29, 2023. Could someone explain what could be the reason?

In this post someone noted that on this day there is a transition between winter and summer time. Maybe this is the reason, but then how to correctly build sequences of dates?

Here is an sample of the code I use:

let data = {
    "2024-03-01": 4,
    "2024-03-02": 1,
    "2024-03-03": 3,
    "2024-03-04": 6,
    "2024-03-05": 2,
    "2024-03-06": 0,
    "2024-03-07": 5,
    "2024-03-08": 1,
    "2024-03-09": 3
}

function adjustDataToHave365days(data) {
    const dates = Object.keys(data).map(date => new Date(date))
    const firstDate = dates[0]

    let startDate = new Date(firstDate)
    startDate.setDate(startDate.getDate() - (365 - dates.length))

    console.log(firstDate)
    console.log(startDate)

    const result = {};
    let currentDate = new Date(startDate);

    while (currentDate <= firstDate) {
        console.log(currentDate)
        const formattedDate = currentDate.toISOString().split('T')[0];
        result[formattedDate] = 0;
        currentDate.setDate(currentDate.getDate() + 1);
    }

    Object.keys(data).forEach(date => {
        resultJuly 5, 2024 = dataJuly 5, 2024;
    });

    return result;
}

console.log(adjustDataToHave365days(data))

Output:

...
  2023-10-26: 0,
  2023-10-27: 0,
  2023-10-28: 0,
  2023-10-30: 0,
  2023-10-31: 0,
  2023-11-01: 0,
...

It just skips 2023-10-29.

I tried different combinations of dates and different algorithms.

2

Answers


  1. Javascript can do strange things with Dates and Strings. In the year-month-day format you are using, it can be one day off because of the time zone.

    To use your example, in my browser I console I entered:

    • new Date("2023-10-29")
    • creates: Sat Oct 28 2023 20:00:00 GMT-0400 (Eastern Daylight Time)

    As you can see, it’s using my local time zone which is subtracting 4 hours. My time is set to 20 instead of 00.

    I would suggest that you add a time to your code. This will force your timezone on the date and fix the issue.

    const dates = Object.keys(data).map(date => new Date(date + "T00:00:00"))

    Change to the above line and it works.

    Login or Signup to reply.
  2. someone noted that on this day there is a transition between winter
    and summer time. Maybe this is the reason

    It is.

    but then how to correctly build sequences of dates?

    Your problem is that you’re working using local date methods, but using UTC dates to format your timestamps. Just use one or the other.

    E.g. to use only local, change:

    const formattedDate = currentDate.toISOString().split('T')[0];
    

    to

    let f = new Intl.DateTimeFormat('en-ca', {
      year: 'numeric',
      month:'2-digit',
      day:'2-digit'
    });
    
    const formattedDate = f.format(currentDate);
    

    thereby using local values and avoiding offset changes.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search