Walker Farrow Walker Farrow - 5 months ago 58
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");

This returns, in the console
"Invalid Date"

I am passing the following format:

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

Any help on this would be appreciated.


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.