Search code examples
javascriptdateutcunix-timestamp

how to load a UTC numeric date and convert it to a unix +new Date()?


I'm using coingecko api and they state that -

Timestamps returned by this API are in UTC Timezone.

and the call I'm making:

'https://api.coingecko.com/api/v3/coins/'+'dash'+'/ohlc?vs_currency='+'eur'+'&days=365'

for example the timestamp (I think) that should be for today is 1579046400000:

new Date(1579046400000)
Wed Jan 15 2020 00:00:00 GMT+0000 (Western European Standard Time)

but today is:

+new Date()
1610300187790
Sun Jan 10 2021 17:36:27 GMT+0000 (Western European Standard Time)

I'm looking at the Date object in javascript https://www.quackit.com/javascript/javascript_date_and_time_functions.cfm

and I'm very confused. How do I convert this UTC timestamp to a normal unix timestamp?

I don't even understand what format the UTC one is, is it milliseconds? (to me it looks like seconds)

all UTC questions I can find the date does not look like this! (They look like strings)

I can't even figure out how to load this number into the date object:

new Date().UTC(579046400000)
VM392:1 Uncaught TypeError: (intermediate value).UTC is not a function
    at <anonymous>:1:12
(anonymous) @ VM392:1
new Date().toUTCString(579046400000)
"Sun, 10 Jan 2021 18:01:25 GMT"
new Date().

setUTCDate(579046400000)
NaN
Date.UTC(579046400000)
NaN

How do I load it into the date object?


Solution

  • The data is sorted from oldest to youngest. The first entry - which you assumed to be today - is actually the oldest one, roughly 365 days old. The last entry in the list is from today. So new Date(timestamps) is the correct way to handle these dates. (UTC and Unix timestamps are identical for everything after 1972 - I believe) – cuzi

    I should not be dyslexic

    @cuzi is 100% correct!