简体   繁体   中英

Get timestamp from yyyy-mm-dd

I need to get unix timestamp from yyyy-mm-dd HH:mm:ss time.

I know There are many ways to get timestamp.

But I need to get timestamp without using new Date() way.

For example,

new Date("2019-10-25 00:00:00").getTime() / 1000

Asia/Seoul time and other countries value return same value.

I also tried to use momentJS with timezone like this.

moment().tz('Asia/Seoul').valueOf()
moment().tz('America/Los_Angeles').valueOf()

How can I get timestamp without using like above code?

As statet in moment-timezone DOCS :

moment.tz(..., String) is used to create a moment with a time zone. It takes all the same arguments as the moment constructor, but uses the last argument as a time zone identifier:

 const timestamp1 = moment.tz("2019-10-25 00:00:00", "Asia/Taipei").format('X'); const timestamp2 = moment.tz("2019-10-25 00:00:00", "America/Toronto").format('X'); console.log(timestamp1); console.log(timestamp2);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.26/moment-timezone-with-data.min.js"></script>

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