Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moment.js transform to date object

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()) 
like image 325
vadim.zhiltsov Avatar asked Aug 01 '13 07:08

vadim.zhiltsov


People also ask

How do you pass the moment to the date?

js parsing date and time. We can parse a string representation of date and time by passing the date and time format to the moment function. const moment = require('moment'); let day = "03/04/2008"; let parsed = moment(day, "DD/MM/YYYY"); console.

Is moment a date object?

Calling the moment() function returns an object that encapsulates the current date and time. const now = moment(); // Moment {_isAMomentObject: true, _isUTC: false, _pf: Object, …} This is a moment. js specific object, not a JavaScript Date object, but it can be easily converted with the toDate() function.

What will Moment () return?

Displaying Formatted Dates Moment. js helps display date with specified formats. moment() returns a date and format() converts the date string tokens and replaces them with specified format values, which are readable.


1 Answers

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) 
like image 191
Chandrew Avatar answered Sep 21 '22 15:09

Chandrew