A
A
Andrew2022-03-15 12:35:56
JavaScript
Andrew, 2022-03-15 12:35:56

Why does .getDay() return the wrong day?

By api ( https://openweathermap.org/forecast5 ) I get timestamps for five days in advance with a difference of three hours. I wanted to sort an array of 40 objects by day, but I ran into an error that .getDay() returns the wrong day

const date = new Date(1647367200 * 1000)
console.log(date); //2022-03-15T18:00:00.000Z

const date1 = new Date(1647378000 * 1000)
console.log(date1); //2022-03-15T21:00:00.000Z

that is, both timestamps are Tuesday March 15, but
const date = new Date(1647367200 * 1000).getDay()
console.log(date); //возвращает 2 - вторник

const date1 = new Date(1647378000 * 1000).getDay()
console.log(date1); //возвращает 3 - среду

With what it can be connected?

If it is important, I sort with this function

const daysObj = {}
        
        this.state.list.forEach(e => {
            const date = new Date(e.dt * 1000).getDay()
            if (typeof daysObj[date] == 'undefined')
                daysObj[date] = [];

            daysObj[date].push(e);
        })

        const sortedDays = Object.keys(daysObj).map(function (key) {
            return daysObj[key];
        });

This.state.list contains the list array from the api response

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2022-03-15
@Rsa97

2022-03-15 T 18 :00:00.000 Z === 2022-03-15 T 21 : 00:00.000 MSK 2022-03-15 T 21 : 00: 00.000 Z === 2022-03-16 T 00 00:00.000 GMT

const date = new Date(1647367200 * 1000);
console.log(date); // Date Tue Mar 15 2022 21:00:00 GMT+0300 (Москва, стандартное время)

const date1 = new Date(1647378000 * 1000)
console.log(date1); // Date Wed Mar 16 2022 00:00:00 GMT+0300 (Москва, стандартное время)

I
Ilya Olovyannikov, 2022-03-15
@cannibal_corpse

It returns everything correctly.
Since you multiply by 1000, you get back different times in hours - 21:00 and 00:00. 00 hours is the next day.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question