如何将UTC时间转换为浏览器本地时区?

时间:2017-08-08 10:25:11

标签: javascript c# jquery asp.net-mvc

我想将UTC时间转换为客户端浏览器时区。

我正在使用以下代码

var utcDate = '08-Aug-17 10:12:29 AM' + ' UTC';
var date = new Date(utcDate);

输出:Tue Aug 08 2017 15:42:29 GMT+0530 (India Standard Time)

预期输出:08-Aug-2017 3:42 PM

如何在javascript中格式化日期?

3 个答案:

答案 0 :(得分:2)



var options = {
    timeZone: "America/New_York",
    year: 'numeric', month: 'numeric', day: 'numeric',
    hour: 'numeric', minute: 'numeric', second: 'numeric'
};

var formatter = new Intl.DateTimeFormat([], options);

var UTCTime = "2017-09-03T02:00:00Z";
var localTime = formatter.format(new Date(UTCTime));
var currentTime = formatter.format(new Date()); console.log(currentTime, localTime);




答案 1 :(得分:1)

为此,您必须使用momentJS
使用momentJS,您可以按照以下方式进行操作

var utcDate = '08-Aug-17 10:12:29 AM' + ' UTC';
console.log(moment(utcDate).format('DD-MMM-YYYY hh:mm:ss a')); 

答案 2 :(得分:0)

您可以将时刻库用于应用程序中出现的大量日期时间相关问题。

moment.utc('Your UTC time here').locale("your culture here").format('llll')

Download momentJs here