j_d
j_d

Reputation: 3082

Convert timestamp from timezone to UTC timestamp?

I am receiving a timestamp from a third-party API, that looks like: 1540388730994. However, I have been informed that this timestamp is in 'Europe/Amsterdam' timezone.

I need to convert this timestamp to UTC, as we store all our dates in UTC.

How is this possible in JavaScript?

So far I have tried:

const timestamp = 1540388730994

const timestampInUTC = moment.tz(timestamp, 'Europe/Amsterdam').utc().valueOf()

console.log(timestamp, timestampInUTC)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.21/moment-timezone-with-data-2012-2022.min.js"></script>

However, you can clearly see that the two output timestamps are identical, whereas I would have expected the conversion to subtract 1-2 hours, as Amsterdam timezone is GMT+2.

What am I doing wrong here?

Upvotes: 1

Views: 4399

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241440

Timestamps in numeric form are always in UTC. If they've for some reason manually added/subtracted it by a time zone offset, they are doing it wrong. You don't see any change from moment, because a UTC timestamp is the same moment in time regardless of what time zone you represent the local time equivalent in. If you expected the timestamp to change, that would be representing an entirely different moment in time.

Upvotes: 3

Related Questions