Walker Farrow Walker Farrow - 1 year ago 136
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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download