Angular 4 Date Pipe转换错误 [英] Angular 4 Date Pipe converting wrongly

查看:202
本文介绍了Angular 4 Date Pipe转换错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有rest服务,该服务返回对象的集合,并且该项目的字段之一是日期字符串(ISO-8601格式)和日期值,如下所示:

I have rest service which returns a collection of objects and one of the field of the item is a date string (ISO-8601 format ) and the date value as follows

"createdDate":"2017-02-21T12:56:50.907",

"createdDate" : "2017-02-21T12:56:50.907",

在angular4用户界面中,我将DatePipe设置为上述日期的格式

In the angular4 UI I put DatePipe to format the above date

{{resultItem.createdDate| date:'short'}}

我得到如下错误的转换

2017年2月21日,上午7:56

2/21/2017, 7:56 AM

代替

2/21/2017,0:56 AM

2/21/2017, 0:56 AM

推荐答案

您可能需要根据带时区的日期创建一个UTC日期...我假设您位于太平洋时区,因为该时间距UTC 7小时. ..

You may need to create a UTC date from your date with timezone... I assume you are in the pacific timezone as the time is 7 hours from UTC...

尝试以下代码以获取不带时区的新日期对象(假设您的变量名为日期"):

Try this code to get a new date object without Timezone (assuming your variable is named "date"):

var datewithouttimezone = new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(),  date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());

这篇关于Angular 4 Date Pipe转换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆