kkangil
kkangil

Reputation: 1746

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?

Upvotes: 0

Views: 153

Answers (1)

Mosè Raguzzini
Mosè Raguzzini

Reputation: 15871

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>

Upvotes: 1

Related Questions