I hope this was useful.I've been having some trouble trying to get a Date object in TypeScript to format the way I want it to. var format moment ().localeData ('en').longDateFormat ('L') moment ().format (format).substr (0,moment ().format (format).length-5) the year doesn't always go in the same spot - could be in the beginning or the end. 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. It might be a bit more hacky than you're looking for, but. 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. For the sake of clarity, newYork.toDate()//will give you the Greenwich ,UK, time. utc(true) ,with the arg true, to your moment object. 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. 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 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!). First you should understand how to use moment-timezone. Unless absolutely necessary, you should use a key such as America/Denver. They stem from POSIX style time zones, and only a few of them are in the TZDB data. Parsing string to date (meaning input is str, out is date) You need to use the. Time Zones like MST7MDT are there for backwards compatibility reasons. If that's not what you expected, you should use the. Format date with Moment.js Ask Question Asked 10 years, 5 months ago Modified 11 months ago Viewed 1.1m times 368 I have a string in this format: var testDate 'Fri 19:08:55 GMT-0500 (CDT)' I would like to use Moment.js get it in this format mm/dd/yyyy : for display. If you are near to the date, it will return a value like 'Today 9:00 AM'. As a result i'm writing: const IsoDateTo moment (dateTo).format ('YYYY-MM-DD THH:mm:ss') The date to is but the IsoDateTo is returning something like this: T00:00:00 Also when i enter a date. I want to transform it to iso using moment again. What am I doing wrong And I would take any other easier solutions as well. i have a date which i formated using moment to be shown like this. You will find moment's parser to be much more reliable. Instead, provide a format string that matches the expected input, such as: moment(' 9:00', 'M/D/YYYY H:mm'). I am currently using Moment js to parse an ISO 8601 string into date and time, but it is not working properly. import moment from 'moment-timezone' // using utc time here const time moment.tz ('T02:08:10.370Z') const localtz moment.tz.guess () const date time.clone ().tz (localtz) const formatDate moment (date).format ('MMMM Do YYYY, h:mm:ss A z') console.log (formatDate. If you are parsing from a string, pass that string directly into moment. the best way to get a user's time zone is using moment-timezone. 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(). As long as you have initialized moment-timezone with the data for the zones you want, your code works as expected.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |