Momentjs: How to convert date/time of one timezone to UTC date/time
Asked Answered
C

5

7

I have a date/time with a timezone and want to convert it into UTC

const date = '2019-04-10T20:30:00Z';
const zone = 'Asia/Kuala_Lumpur';
const utcDate = moment(date).tz(zone).utc().format();
console.log('UTC Date : ', utcDate);

is my date variable is in standard formate for UTC? How to cast this time zone to another time zone?

Cispadane answered 10/4, 2019 at 12:1 Comment(4)
what is the question actually??Forzando
Is the problem that '2019-04-10T20:30:00Z' is already the standard format for that datetime in UTC? And that when you cast it to Asia time, it's wrong becasue you assume '2019-04-10T20:30:00Z' is local time?Uncurl
@naibkhan I have a date/time with timzone(It's random or dynamic) and want to convert it into UTCCispadane
Datetime strings containing T between the date and time and ending in Z is the international standard for UTC time. ISO 8601 . So if it's supposed to be local time, try fixing whatever generates these random/dynamic datetime strings. You could like, remove the 'Z' from the end to make sure the string gets parsed as local time first.Uncurl
M
13

The UTC timezone is denoted by the suffix "Z" so you need to remove "Z" and use moment.tz(..., String) instead of moment().tz(String) because the first create a moment with a time zone and the second is used to change the time zone on an existing moment:

const date = '2019-04-10T20:30:00';
const zone = 'Asia/Kuala_Lumpur';
const utcDate = moment.tz(date, zone).utc().format();
console.log('UTC Date : ', utcDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.23/moment-timezone-with-data.min.js"></script>
Michaelis answered 10/4, 2019 at 12:17 Comment(0)
E
1
function calcTime(city, offset) {

     // create Date object for current location
     var d = new Date();

     // convert to msec
     // add local time zone offset
     // get UTC time in msec
     var utc = d.getTime() + (d.getTimezoneOffset() * 60000);

     // create new Date object for different city
     // using supplied offset
     var nd = new Date(utc + (3600000*offset));

    // return time as a string
    return "The local time in " + city + " is " + nd.toLocaleString();
}
Enneagon answered 15/1, 2020 at 9:55 Comment(1)
When answering an old question, your answer would be much more useful to other StackOverflow users if you included some context to explain how your answer helps, particularly for a question that already has an accepted answer. See: How do I write a good answer.Tilly
H
0

const date = new Date();
console.log(date);
const zone = 'Asia/Karachi';
const utcDate = moment.tz(date, zone).utc().format();
console.log('UTC Date : ', utcDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.23/moment-timezone-with-data.min.js"></script>
Horne answered 21/9, 2021 at 21:16 Comment(0)
F
-1

You can do this like the code bellow:

  // your inputs
    var date  = '2019-04-10T20:30:00Z';
    var desiredFormate   = "MM/DD/YYYY h:mm:ss A";  // must match the input
    var zone  = 'Asia/Kuala_Lumpur';

    // construct a moment object
    var m = moment.tz(date , desiredFormate, zone);
    // convert it to utc
    m.utc();

    // format it for output
    var s = m.format(fmt)  // result: 2017-08-31T08:45:00+06:00
Forzando answered 10/4, 2019 at 12:15 Comment(0)
C
-1

You could use moment.js documentation:

moment().format('MMMM Do YYYY, h:mm:ss a'); // April 10th 2019, 3:29:36 pm
moment().format('dddd'); // Wednesday
moment().format("MMM Do YY"); // Apr 10th 19
moment().format('YYYY [escaped] YYYY'); // 2019 escaped 2019
moment().format();
Circinate answered 10/4, 2019 at 12:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.