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
4 Replies
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)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
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)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