将UTC GMT + 0200转换为当地时间

时间:2017-01-10 12:29:44

标签: momentjs

我正在尝试将DateTime中存储为SQL Server的UTC日期转换为客户端的本地 DateTime

我的日期以UTC格式存储在我的数据库中,其值为2017-01-10 10:52:07.820

使用下面的代码显示为Tue Jan 10 2017 10:52:07 GMT+0200 (GTB Standard Time)

 var testDateUtc = moment(item.DatePosted); //"/Date(1484038327820)/"
 var localDate = moment(testDateUtc).local();           
 var d = localDate.toDate();              
 var DatePosted = d; //result is Tue Jan 10 2017 10:52:07 GMT+0200 (GTB Standard Time)

我希望将偏移量GMT + 0200添加到日期,以便最终日期如下:2017年1月10日星期二 12 :52:07或理想情况下将其格式化为:{{1 }}

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

由于您输入的是UTC,因此在构建时刻对象时必须使用moment.utc模式。

您可以使用format()选择如何显示时刻对象。

这是一个工作示例:

var testDateUtc = moment.utc(1484038327820);
var localDate = moment(testDateUtc).local();
console.log(localDate.format('D/M/YYYY HH:mm'));
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>