Moment.js Convert Local time to UTC time does work
I would like to use Moment.js to convert a local time to UTC equivalent. I believe that I have the correct method in place, but it does not alter the time.
I'm in Sydney Australian +11 and expect the UTC time to be 11 hours earlier.
Internally on the moment object the isUTC flag changes from false to true, but the time does NOT shift, am I meant to use a different technique for this.
How do I actually get the current UTC date out of this object
var val = '18/03/2015'; var selectedDate = moment(val, 'DD/MM/YYYY');
var a = selectedDate.utc()
I just tried this code and it seems like I get the correct UTC time. I guess I just want to confirm that what I am doing is correct way to access the UTC time from moment.js
I found that my usage pattern of in my application was incorrect
It should have been
Convert date to UTC using moment.js, Assuming the input text is entered in terms of the users's local time: ValueOf does not work for me. As of : moment.js version 2.24.0. let's say� Since UTC is implied by the output, it would be redundant to call utc() again. On Item 2 - Just like the utc() function, there's also a local() function. Once you have a moment object, you can use toDate or format or any other of the functions described in the documentation. No, you do not need to create a new moment using the generated date
The question is old, but I also faced it. It may be useful to someone:
Using the method of utcOffset() to calculate the UTC time:
selectedDate = (moment(selectedDate).add(-(moment().utcOffset()), 'm'));
And explicitly specify UTC:
selectedDate = moment.parseZone(selectedDate).utc().format();
Guides, All code should work in both of these environments, and all unit tests are run in both of Unambiguous input (with offset) is adjusted to local time. moment.utc(. You can easily convert the date format (mm/dd/yyyy, mm-dd-yyyy, DD-MM-YYYY, yyyy-mm-dd) to any particular format using moment js. You get the current date and time using moment() method and format the current date and time to specified format using format() method.
This worked for me !!
selectedDate = moment(selectedDate).add(moment(selectedDate).utcOffset(), 'm').utc().format()
Create a local moment object from you local time and convert it to UTC then format it, then create a new UTC moment from that formatted UTC string
var localDateString = '24/04/2019'; var localDateStringFormat = 'DD/MM/YYYY'; var utcMoment = moment.utc(moment(localDateString, localDateStringFormat ).utc().format('YYYY-MM-DD HH:mm:ssZ')) console.log(utcMoment);
Docs, I have a simple use case but I cannot figure out how to convert from GMT/UTC to local time in moment.js. Example: var gmtDateTime = moment.utc("2015-10-24 20:00", "YYYY-MM-DD HH").format('YYYY-MMM-DD h:mm A'); console.log(gmtDateTime) emits 2015-Oct-24 8:00 PM, which is correct. Now I simply want to convert this to my local time, which happens
After few frustrating hours, I found what was the problem
Short Answer: To convert time to utc, we need to use
Long Answer: Take the example
.utc sets the isUTC flag to true.
When logging the date, the
d key always shows the time in local timezone. (Which makes us believe its not working properly - as shown in your screenshot)
But we need to use .format to get the date/time in UTC format.
The above code returns
June 3, 2019 6:30 PM which is the correct UTC time.
local to UTC to local date time conversion giving wrong time � Issue , This brings us to an interesting feature of Moment.js. UTC mode. While in UTC mode, all display methods will display in UTC time instead of local time. moment().format(); // 2013-02-04T10:35:24-08:00 moment.utc().format(); // 2013-02-04T18:35:24+00:00
There are two interfaces for using time zones with Moment.js. moment.tz(, String) does parsing in given time zone It takes all the same arguments as the moment constructor, but uses the last argument as a time zone identifier:
The moment.tz() function will parse a given time with offset and convert it to the time zone provided. It is useful when you need to interpret a time in a time zone other than UTC, or the user’s local time. Note that this functionality does not work without the Moment TimeZone add on library being included in your project.
- That last code snippet is correct. Take a look at momentjs.com/docs/#/parsing/utc . In order to get into utc mode, you should create a new moment. Though in my own testing, I seem to be able to call
.utc()on an existing moment successfully, so who knows.
moment.utc(..., I guess :)
- @medik is right, you should use
- if selectedDate be a timestamp in milisecond, it dos not work. why?
- I think I had the reverse problem... dates, in JSON, were utc but interpreted as local, so I had to add the utc offset. One thing though, I think you really want to use the selectedDate to determine the offset as it will change... this was my code: const correctedDateTime = m.add((moment(utcDate).utcOffset()), 'm');
- This does not work if the offset needs to be subtracted.