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.
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.
Share Improve this question asked Oct 16, 2021 at 5:26 Siddu PalaparthiSiddu Palaparthi 891 gold badge3 silver badges11 bronze badges 2-
Did you try doing
moment('str_time', "YYYY-MM-DD").unix()
? – thisdotutkarsh Commented Oct 16, 2021 at 5:31 - 1 Just remember: the moment.js documentation also says to stop using it, because time passed it by =) – Mike 'Pomax' Kamermans Commented Oct 16, 2021 at 5:50
3 Answers
Reset to default 3Unix 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./ajax/libs/moment.js/2.29.1/moment.min.js"></script>
Call unix()
on moment
object. Reference.
moment('2020-12-12').unix()
To create a Unix timestamp (seconds since the Unix Epoch) from a moment, use the following,
console.log(moment('2021-10-16').unix());
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.29.1/moment.min.js"></script>
Since your date format follows the ISO_8601 format, i.e. YYYY-MM-DD you do not need to provide the input date format to the moment constructor.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744059781a4551568.html
评论列表(0条)