Walker Farrow Walker Farrow - 1 month ago 20
Javascript Question

Extract epoch from moment object

I am trying to extract the unix timestamp (epoch) from a date string as follows:


var week = $("#week").val();
var timestamp = moment(week).format("X");
console.log(timestamp);


This returns, in the console
"Invalid Date"
.

I am passing the following format:
"03-Nov-16"


I am trying to return the unix timestamp of a date.

Any help on this would be appreciated.

Answer

You need to tell moment the format of the date string you're trying to parse. I think you're looking for something like this (not tested):

var timestamp = moment("03-Nov-16", "DD-MMM-YY").unix()

See the official docs on date string parsing for more info.