Convert timestamp from timezone to UTC timestamp?

ischnmn

New Member
#1
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:
Mã:
const timestamp = 1540388730994

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

console.log(timestamp, timestampInUTC)
Mã:
<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?
 

Admin

Administrator
Thành viên BQT
#2
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.
 
Top