Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Before Conversion

var val = '18/03/2015';
var selectedDate = moment(val, 'DD/MM/YYYY');

Before Conversion

After Conversion

var a = selectedDate.utc()

After Conversion

like image 639
David Cruwys Avatar asked Mar 18 '15 05:03

David Cruwys


People also ask

Does moment convert UTC to local?

For moment. utc, the date will be parsed and displayed in local time. By default, date will be shown in local time. In case you need to parse it in UTC, you can make use of this method.

Does moment use UTC by default?

By default, moment parses and displays in local time. If you want to parse or display a moment in UTC, you can use moment. utc() instead of moment() .

Does moment js use local timezone?

Default time zone setDefault(String); By default, moment objects are created in the local time zone. Local time zone - it's a time zone which is set in a browser or on your node. js server.


1 Answers

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

a.format("YYYY-MM-DD HH:mm:ssZ")

"2015-03-18 04:19:46+00:00"

I found that my usage pattern of in my application was incorrect

selectedDate.utc().format(fullFormat)

It should have been

moment.utc(selectedDate).format(fullFormat)
like image 85
David Cruwys Avatar answered Sep 18 '22 09:09

David Cruwys