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. 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. 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. We can use several ways to create date and time Moment.js objects. The date and time parts are separated by T character. node today.js T16:32:53+02:00 This is the ISO standard format. 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. By default, we get a long datetime format. ![]() If you instead want a javascript Date object from a string: const myDate moment (str, 'YYYY-MM-DD'). So you should try using moment's format() method like this: myDate. If you want a moment date from a string: const myMomentObject moment (str, 'YYYY-MM-DD') From moment documentation: Instead of modifying the native Date.prototype, Moment.js creates a wrapper for the Date object. 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. For the sake of clarity, var newYork= moment.tz(" 12:00", "America/New_York") /*this code will consider NewYork as the timezone.*/ This result happens because you are using the toISOString() method and it is not a valid format to insert into your DATETIME column. 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. Time Zones like MST7MDT are there for backwards compatibility reasons. You will find moment's parser to be much more reliable. If you are parsing from a string, pass that string directly into moment. Both will work but it's unnecessarily redundant. The date return contains the Weekday, Month, Day, Year, Time and Timezone Offset. It is similar to the native Date object’s toString () method. While the moment constructor can take a Date, it is usually best to not use one. The moment ().toString () Method is used to return a human-readable string of the Moment object in English. That's a different question, and wouldn't involve the output of the above because: You can't get a timestamp from the output string, you'd get it from a moment object. ![]() Now I need timestamp from the output value using MomentJS. There's nothing moment.js can do about that. That converts a local value to Paris time, and emits it as a string in ISO8601 format. ![]() ![]() What am I doing wrong And I would take any other easier solutions as well. A JavaScript Date object will always be printed in the local time zone of the computer it's running on. I am currently using Moment js to parse an ISO 8601 string into date and time, but it is not working properly. 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 |