Skip to content
Advertisement

How to convert Moment.js moment to unix timestamp

I see a lot in the Moment.js documentation about getting a Moment from a Unix timestamp. However, I am trying to convert a Moment to a Unix timestamp, and I am not sure how to do that. This is how my moment looks:

const myMomentObject = moment(str_time, ‘YYYY-MM-DD’);

And I need to convert it to a Unix timestamp.

Advertisement

Answer

Unix timestamp can be obtaineded with or without the moment library.

//Moment
const myMomentObject = moment('2021-10-16', 'YYYY-MM-DD');
console.log(myMomentObject.unix());


//Vanilla
const d = new Date('2021.10.16');
console.log(d.getTime() / 1000);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement