简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM