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.
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())
Use this to transform a moment object into a date object:
Tue Nov 04 2014 14:04:01 GMT-0600 (CST)
You are correctly converting the moment to the time zone, which is reflected in the second line of output from
Switching to UTC doesn’t just drop the offset, it changes back to the UTC time zone. If you’re going to do that, you don’t need the original
.tz() call at all. You could just do
Perhaps you are just trying to change the output format string? If so, just specify the parameters you want to the
Regarding the last to lines of your code – when you go back to a
Date object using
Date object will always be printed in the local time zone of the computer it’s running on. There’s nothing moment.js can do about that.
A couple of other little things:
While the moment constructor can take a
Date, it is usually best to not use one. For “now”, don’t use
moment(new Date()). Instead, just use
moment(). Both will work but it’s unnecessarily redundant. If you are parsing from a string, pass that string directly into moment. Don’t try to parse it to a
Datefirst. You will find moment’s parser to be much more reliable.
Time Zones like
MST7MDTare there for backwards compatibility reasons. They stem from POSIX style time zones, and only a few of them are in the TZDB data. Unless absolutely necessary, you should use a key such as
.toDate did not really work for me, So, Here is what i did :
futureStartAtDate = new Date(moment().locale("en").add(1, 'd').format("MMM DD, YYYY HH:MM"))
hope this helps
const currentTime = new Date(); const convertTime = moment(currentTime).tz(timezone).format("YYYY-MM-DD HH:mm:ss"); const convertTimeObject = new Date(convertTime);
I needed to have timezone information in my date string. I was originally using
moment.tz(dateStr, 'America/New_York').toString(); but then I started getting errors about feeding that string back into moment.
I tried the
moment.tz(dateStr, 'America/New_York').toDate(); but then I lost timezone information which I needed.
The only solution that returned a usable date string with timezone that could be fed back into moment was
moment has updated the js lib as of 06/2018.
var newYork = moment.tz("2014-06-01 12:00", "America/New_York"); var losAngeles = newYork.clone().tz("America/Los_Angeles"); var london = newYork.clone().tz("Europe/London"); newYork.format(); // 2014-06-01T12:00:00-04:00 losAngeles.format(); // 2014-06-01T09:00:00-07:00 london.format(); // 2014-06-01T17:00:00+01:00
if you have freedom to use Angular5+, then better use datePipe feature there than the timezone function here. I have to use moment.js because my project limits to Angular2 only.