Moment.js Convert Local time to UTC time does work
Asked Answered
A

10

24

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

Ambassador answered 18/3, 2015 at 5:37 Comment(1)
Does this answer your question? How to convert from GMT/UTC to local time and back using momentjs?Vandalism
A
23

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)
Ambassador answered 18/3, 2015 at 5:41 Comment(4)
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.Kinney
not moment().utc(..., but moment.utc(..., I guess :)Bigoted
@Bigoted is right, you should use moment.utc(datetime).format(MOMENT_UTC_DATE_TIME_FORMAT)Killigrew
if selectedDate be a timestamp in milisecond, it dos not work. why?Flange
T
11

This works

moment(date_to_convert).utc().format("YYYY-MM-DD HH:mm:ss");
Torch answered 15/6, 2020 at 8:39 Comment(0)
I
3

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();
Iscariot answered 2/10, 2017 at 21:58 Comment(1)
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');Sihonn
I
3

This is how you do it using moment-timezone

moment.tz(localDate, localTimeZone).utc()
Invalidate answered 29/6, 2020 at 16:53 Comment(0)
S
2

This worked for me !!

selectedDate = moment(selectedDate).add(moment(selectedDate).utcOffset(), 'm').utc().format()
Skewback answered 1/11, 2017 at 5:3 Comment(2)
This does not work if the offset needs to be subtracted.Killick
It does work for the subtraction cases as the utcOffset returns a negative value for such casesQuartic
C
2

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);
<script src="https://momentjs.com/downloads/moment.js"></script>
Commendatory answered 25/4, 2019 at 12:57 Comment(0)
L
2

After few frustrating hours, I found what was the problem

Short Answer: To convert time to utc, we need to use format()

Long Answer: Take the example

moment.utc(1559586600000).format('LLL')

.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.

Lowercase answered 19/6, 2019 at 6:23 Comment(0)
P
0
const moment = require('moment-timezone');
const dateTime='2020-12-21'
const timezone='America/Anchorage'
const dateTimeInUtc = moment(dateTime).tz(timezone).utc().format();
console.log('dateTimeInUtc',dateTimeInUtc);
Pothole answered 4/1, 2021 at 7:57 Comment(0)
S
0

After few frustrating hours, I found what was the problem

Short Answer: To convert time to utc, we need to use format()

Long Answer: Take the example

moment.utc(1559586600000).format('LLL')

.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.

Scagliola answered 28/11, 2022 at 10:12 Comment(0)
S
0

const moment = require('moment-timezone');
const dateTime='2020-12-21'
const timezone='America/Anchorage'
const dateTimeInUtc = moment(dateTime).tz(timezone).utc().format();
console.log('dateTimeInUtc',dateTimeInUtc);
Scagliola answered 28/11, 2022 at 10:15 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Polycarp
You should probably merge this with your earlier answer, or otherwise explain how the two different answers relate to each other.Causation

© 2022 - 2024 — McMap. All rights reserved.