Javascript 时刻 Js UTC 到本地时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32540667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 10:34:13  来源:igfitidea点击:

Moment Js UTC to Local Time

javascriptdatemomentjs

提问by brian Scroggins

I'm trying to convert UTC time to the local time. I've been following this example from this link: http://jsfiddle.net/FLhpq/4/light/. I can't seem to get the right local output. For example, if its 10: 30 am in here, instead of getting 10:30 ill get 15: 30. Here is my code:

我正在尝试将 UTC 时间转换为本地时间。我一直在通过这个链接关注这个例子:http: //jsfiddle.net/FLhpq/4/light/。我似乎无法获得正确的本地输出。例如,如果这里是上午 10:30,而不是 10:30 生病,而是得到 15:30。这是我的代码:

var date = moment.utc().format('YYYY-MM-DD HH:mm:ss');

var localTime  = moment.utc(date).toDate();

localTime = moment(localTime).format('YYYY-MM-DD HH:mm:ss');

console.log("moment: " + localTime);

No matter what I do the time always comes out at UTC time. I live in Houston so I know timezone is the issue. I've followed the code in the link but can seem to get the local time. What am I doing wrong?

无论我做什么,时间总是在 UTC 时间出现。我住在休斯顿,所以我知道时区是问题所在。我已经按照链接中的代码进行操作,但似乎可以获得当地时间。我究竟做错了什么?

回答by axon

To convert UTC time to Local you have to use moment.local().

要将 UTC 时间转换为本地时间,您必须使用moment.local().

For more info see docs

有关更多信息,请参阅文档

Example:

例子:

var date = moment.utc().format('YYYY-MM-DD HH:mm:ss');

console.log(date); // 2015-09-13 03:39:27

var stillUtc = moment.utc(date).toDate();
var local = moment(stillUtc).local().format('YYYY-MM-DD HH:mm:ss');

console.log(local); // 2015-09-13 09:39:27

Demo:

演示:

var date = moment.utc().format();
console.log(date, "- now in UTC"); 

var local = moment.utc(date).local().format();
console.log(local, "- UTC now to local"); 
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

回答by JAMZAD

Try this:

尝试这个:

let utcTime = "2017-02-02 08:00:13";

var local_date= moment.utc(utcTime ).local().format('YYYY-MM-DD HH:mm:ss');

回答by Abdur Rahim

let utcTime = "2017-02-02 08:00:13.567";
var offset = moment().utcOffset();
var localText = moment.utc(utcTime).utcOffset(offset).format("L LT");

Try this JsFiddle

试试这个JsFiddle

回答by Brady Huang

To convert UTC to local time

将 UTC 转换为本地时间

let UTC = moment.utc()
let local = moment(UTC).local()

Or you want directly get the local time

或者你想直接获取当地时间

let local = moment()

var UTC = moment.utc()
console.log(UTC.format()); // UTC time

var cLocal = UTC.local()
console.log(cLocal.format()); // Convert UTC time

var local = moment();
console.log(local.format()); // Local time
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

回答by Fung LAM

Note: please update the date format accordingly.

注意:请相应地更新日期格式。

Format Date

格式日期

   __formatDate: function(myDate){
      var ts = moment.utc(myDate);
      return ts.local().format('D-MMM-Y');
   }

Format Time

格式化时间

  __formatTime: function(myDate){
      var ts = moment.utc(myDate);
      return ts.local().format('HH:mm');
  },

回答by Rohit Parte

I've created one function which converts all the timezones into local time.

我创建了一个将所有时区转换为本地时间的函数。

Requirements:

要求:

1. npm i moment-timezone

function utcToLocal(utcdateTime, tz) {
    var zone = moment.tz(tz).format("Z") // Actual zone value e:g +5:30
    var zoneValue = zone.replace(/[^0-9: ]/g, "") // Zone value without + - chars
    var operator = zone && zone.split("") && zone.split("")[0] === "-" ? "-" : "+" // operator for addition subtraction
    var localDateTime
    var hours = zoneValue.split(":")[0]
    var minutes = zoneValue.split(":")[1]
    if (operator === "-") {
        localDateTime = moment(utcdateTime).subtract(hours, "hours").subtract(minutes, "minutes").format("YYYY-MM-DD HH:mm:ss")
    } else if (operator) {
        localDateTime = moment(utcdateTime).add(hours, "hours").add(minutes, "minutes").format("YYYY-MM-DD HH:mm:ss")
    } else {
        localDateTime = "Invalid Timezone Operator"
    }
    return localDateTime
}

utcToLocal("2019-11-14 07:15:37", "Asia/Kolkata")

//Returns "2019-11-14 12:45:37"