Issues with time (rfc2822Date to longformat)

im getting time from api's and i wanted to change the styling of time from Sat, 25 Mar 2023 10:05:20 GMT to Saturday, March 25 at 3:35 PM the only issue is the time, it has to be 10:05am instead its taking 3:35pm. i dont know where this 3:35is getting from, im not in that timezone etc. this is the code which im using
let epochtime = weatherData.dt;
let timezone = weatherData.timezone;
const rfc2822Date = new Date((epochtime + timezone) * 1000).toUTCString();

//change rfc date to long readable format

const date = new Date(rfc2822Date);
const options = {
weekday: "long",
month: "long",
day: "numeric",
hour: "numeric",
minute: "numeric",
hour12: true
};
const longFormatDateTime = date.toLocaleString("en-US", options);
dateTime.innerText = longFormatDateTime;

console.log(rfc2822Date)
console.log(longFormatDateTime)
let epochtime = weatherData.dt;
let timezone = weatherData.timezone;
const rfc2822Date = new Date((epochtime + timezone) * 1000).toUTCString();

//change rfc date to long readable format

const date = new Date(rfc2822Date);
const options = {
weekday: "long",
month: "long",
day: "numeric",
hour: "numeric",
minute: "numeric",
hour12: true
};
const longFormatDateTime = date.toLocaleString("en-US", options);
dateTime.innerText = longFormatDateTime;

console.log(rfc2822Date)
console.log(longFormatDateTime)
4 Replies
13eck
13eck2y ago
Without knowing what weatherData.dt or weatherData.timezone is I can only guess, but my guess is the issues is either in the .toTCString() in your rfc2822Date or in the date.toLocaleString(). One or the other is treating GMT as local as it looks like an issue of timezone offset. If you could provide the missing info I can run some local tests to find the error (Also, this is why all date/time data should be stored as UTC/GMT and let the dev worry about timezones :p)
Avinash
AvinashOP2y ago
let say im getting weatherData.dt = 1679757855 , weatherData.timezone = 3600. the local time of this place is around 4:25pm the console log shows the correct solution partially correct local time is Sat, 25 Mar 2023 16:24:15 GMT this is in UTCstring. i changed this into normal date format since longformat function is not changing the original utc string since it doesnt have gmt + local timezone at the end. so i got this Sat Mar 25 2023 21:54:15 GMT+0530 (India Standard Time) then the localestring is applied and got the time Saturday, March 25 at 9:54 PM which is my timezone. @cvanilla13eck
13eck
13eck2y ago
That looks right to me. UTC/GMT + your timezone offset in the above image is 9:54pm And when I run your code in my console I'm getting a like answer: Sat, 25 Mar 2023 16:24:15 GMT Saturday, March 25 at 12:24 PM (at GMT-4, US Eastern Standard Time)
Avinash
AvinashOP2y ago
it has to be 4:24pm instead of 12:24 pm. before converting into long format the time is 4:24pm GMT which is correct. after converting it my time zone is also adding to it and returing long day string @cvanilla13eck
Want results from more Discord servers?
Add your server