

- #Moment format to date string how to
- #Moment format to date string install
- #Moment format to date string code
The output format should be specified when calling. In short, moment.tz considers the time zone you specify and compares your local time with the time in Greenwich to give you a result. let myDate:Date moment (dateString,'YYYY-MM-DD').format ('DD-MM-YYYY') Basically, when you call moment (), you should specify the format your current date is in, not the output format that you want. according to the moment.tz method arg we specified above, it is 12:00.you can ofcourse change this by using moment() NewYork.utc(true).toDate()//will give you the local time. Convert into separate date and time into one string with format moment js. For the sake of clarity, newYork.toDate()//will give you the Greenwich ,UK, time. If I have given date string MM-DD-YYYY and time string HH:mm A or H:m a. So you should try using moment's format() method like this: myDate. The correct format probably is YYYY-MM-DD HH:MM:SS(I think it depends on MySQL configuration, but this is the default) as the docs points out. utc(true) ,with the arg true, to your moment object. This result happens because you are using the toISOString() method and it is not a valid format to insert into your DATETIME column. However if you just want your local time in this format (New York time, according to this example), just add the method. For more details, go through this article, about UTC. Now when you try to convert newYork (the moment object) with moment's toDate() (ISO 8601 format conversion) you will get the time of Greenwich,UK.

#Moment format to date string code
For the sake of clarity, var newYork= moment.tz(" 12:00", "America/New_York") /*this code will consider NewYork as the timezone.*/ This function will return a moment object with a particular time zone. Now in order to use the timezone feature, use moment.tz("date_string/moment()","time_zone") (visit for more details). For the sake of clarity, const moment=require('moment-timezone')//import from moment-timezone
#Moment format to date string install
According to this answer here TypeError: moment().tz is not a function, you have to import moment from moment-timezone instead of the default moment (ofcourse you will have to npm install moment-timezone first!).
#Moment format to date string how to
First you should understand how to use moment-timezone. Unless absolutely necessary, you should use a key such as America/Denver. The correct way to parse an input moment as UTC in the format you provided would be like this: moment. Unlike Date, moment will always be consistent about how it parses. They stem from POSIX style time zones, and only a few of them are in the TZDB data. If the string is specifically YYYY-MM-DD, using hyphens, or if it is YYYY-MM-DD HH:mm:ss, it will interpret it as local time. Time Zones like MST7MDT are there for backwards compatibility reasons. You will find moment's parser to be much more reliable. Of course it does: format returns a string, and you're using format to get enddate. Second: console.log((enddate)) It returns object String even with a valid date, so the if condition doesn't working in my case. How can I get that word to display as a word instead of the 'a' in 'at' being translated to the. If you use a valid date (such as your T00:00:00 example), the code is fine. D, YYYY at h:mm A z') Everything works great except for the word 'at'. 27, 2015 at 8:17 AM' that I need to display using moment.js. If you are parsing from a string, pass that string directly into moment. Moment.js Include text in middle of date format. Both will work but it's unnecessarily redundant. While the moment constructor can take a Date, it is usually best to not use one.

There's nothing moment.js can do about that. A JavaScript Date object will always be printed in the local time zone of the computer it's running on. Regarding the last to lines of your code - when you go back to a Date object using toDate(), you are giving up the behavior of moment.js and going back to JavaScript's behavior. Perhaps you are just trying to change the output format string? If so, just specify the parameters you want to the format method: momentObj.format("YYYY-MM-DD HH:mm:ss") If you're going to do that, you don't need the original. Switching to UTC doesn't just drop the offset, it changes back to the UTC time zone. You are correctly converting the moment to the time zone, which is reflected in the second line of output from momentObj.format(). $(document).As long as you have initialized moment-timezone with the data for the zones you want, your code works as expected. This code is working, but i think it's can be write more simple using moment.js. I try to convert a dateTime string format( dd/MM/yyyy HH:mm:ss), to local user time.
