最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Momentjs: How to convert datetime of one timezone to UTC datetime - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited Apr 10, 2019 at 13:19 naib khan 1,11811 silver badges19 bronze badges asked Apr 10, 2019 at 12:01 Shivprsad SammbhareShivprsad Sammbhare 4303 gold badges7 silver badges22 bronze badges 4
  • what is the question actually?? – naib khan Commented Apr 10, 2019 at 12:03
  • 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? – Shilly Commented Apr 10, 2019 at 12:07
  • @naibkhan I have a date/time with timzone(It's random or dynamic) and want to convert it into UTC – Shivprsad Sammbhare Commented Apr 10, 2019 at 12:08
  • 1 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. – Shilly Commented Apr 10, 2019 at 12:09
Add a comment  | 

5 Answers 5

Reset to default 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>

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();
}

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>

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

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();
发布评论

评论列表(0)

  1. 暂无评论