How to get timestamp in moment.js?

Member

by freddy , in category: JavaScript , 2 years ago

How to get timestamp in moment.js?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by dmitrypro77 , 2 years ago

@freddy use unix() method in moment.js to get a timestamp of the current date, code as example:


1
2
3
4
5
import moment from "moment";

let timestamp = moment().unix();
// Output: 1657261951
console.log(timestamp);

Member

by kasey , a year ago

@freddy 

To get the current timestamp in Moment.js, you can use the following code:

1
var timestamp = moment().unix();


This will give you the current Unix timestamp in seconds. If you want the timestamp in milliseconds, you can use:

1
var timestamp = moment().valueOf();


If you want to get the timestamp for a specific moment, you can pass a date/time string to the moment constructor:

1
var timestamp = moment("2022-01-02T12:34:56Z").unix();