I've been chasing my tail with JavaScript Date and thought I ask here to see if anyone could help.
From a Unix epoch value (millisonds), I'd like to extract the date components (day, month, ..., hours, min) to display them in JavaScript.
For instance this Epoch time 1674434520
(23 January 2023 00:42:00) should give
day=23
month=1 (or 0, when 0-based, I don't mind)
year=2023
hour=0
minute=42
The problem is that in order to convert all the datetime components, I need to convert the Unix epoch number into a JavaScript date (new Date(epoch)
) at which time, it'll assume my browser's local time zone settings (which is not GMT) [1].
I'd like to be able to extract the datetime components regardless of the local browser settings.
Any idea how this can be achieved without external libraries? If possible, I would like to avoid unnecessary parsing/formatting ( Intl.DateTimeFormat
approach) or shifting of the date (to avoid potential leap year/daylight saving issues).
[1] when displaying the dates using devtools or accessing getHours(), etc (clarified what I meant by "assume" based on @Bergi's comment).