Answer the question
In order to leave comments, you need to log in
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
const date = new Date(1647367200 * 1000).getDay()
console.log(date); //возвращает 2 - вторник
const date1 = new Date(1647378000 * 1000).getDay()
console.log(date1); //возвращает 3 - среду
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];
});
Answer the question
In order to leave comments, you need to log in
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 (Москва, стандартное время)
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 questionAsk a Question
731 491 924 answers to any question