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())
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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With