Moment.js transform to date object

vadim.zhiltsov picture vadim.zhiltsov · Aug 1, 2013 · Viewed 668k times · Source

Using Moment.js I can't transform a correct moment object to a date object with timezones. I can't get the correct date.

Example:

var oldDate = new Date(),
    momentObj = moment(oldDate).tz("MST7MDT"),
    newDate = momentObj.toDate();
console.log("start date " + oldDate)
console.log("Format from moment with offset " + momentObj.format())
console.log("Format from moment without offset " + momentObj.utc().format())
console.log("(Date object) Time with offset " + newDate)
console.log("(Date object) Time without offset "+ moment.utc(newDate).toDate())

Answer

Chandrew picture Chandrew · Nov 4, 2014

Use this to transform a moment object into a date object:

From http://momentjs.com/docs/#/displaying/as-javascript-date/

moment().toDate();

Yields:

Tue Nov 04 2014 14:04:01 GMT-0600 (CST)