Angular DatePipe-将秒转换为时区为零的时间(12而不是00) [英] Angular DatePipe - Convert seconds to time with zero timezone (12 instead of 00)

查看:70
本文介绍了Angular DatePipe-将秒转换为时区为零的时间(12而不是00)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用DatePipe转换数字(代表秒)得到这样的结果:

00:00:05

我已经尝试通过DatePipe这样做:

 < label>无时区(默认){{5 * 1000 |日期:'h:mm:ss'}}</label> 

但是它会产生包含时区+4的结果(这不是我想要的):

4:00:05

因此,为了避免这种情况,我正在尝试将时区偏移量设置为0:

 < label> {{5 * 1000 |date:'h:mm:ss z':'+ 0000'}}</label>< label> {{5 * 1000 |date:'h:mm:ss z':'UTC'}}</label>< label> {{5 * 1000 |date:'h:mm:ss z':'GMT'}}</label>< label> {{5 * 1000 |date:'h:mm:ss z':'GMT + 0'}}</label>< label> {{5 * 1000 |date:'h:mm:ss z':'UTC + 0'}}</label> 

但是这次时间偏移为12小时而不是0:

12:00:05 GMT + 0

那么有没有一种方法可以使用DatePipe将秒转换为时间而不改变时区?
还是有其他任何角度的管道可用于此目的?

解决方案

因此,这个非常棘手的问题具有非常简单的解决方案.
我们只需要使用"H"而不是"h"(范围为0-23)即可.
示例:

 < label> {{5 * 1000 |date:'H:mm:ss':'UTC'}}</label> 

获取'12'的原因是在时间格式中使用了'h',该时间格式

I want to convert number (which represents seconds) using DatePipe to get result like this :

00:00:05

I've tried doing so with DatePipe this way:

<label>No timezone (default) {{ 5 * 1000 | date:'h:mm:ss'}}</label>

But it produces result with timezone +4 included (which is not what I want):

4:00:05

So in order to avoid this I'm trying to set timezone offset to 0:

<label>{{ 5 * 1000 | date:'h:mm:ss z':'+0000'}}</label>
<label>{{ 5 * 1000 | date:'h:mm:ss z':'UTC'}}</label>
<label>{{ 5 * 1000 | date:'h:mm:ss z':'GMT'}}</label>
<label>{{ 5 * 1000 | date:'h:mm:ss z':'GMT+0'}}</label>
<label>{{ 5 * 1000 | date:'h:mm:ss z':'UTC+0'}}</label>

But this time the time is shifted with 12 hours instead of 0:

12:00:05 GMT+0

So is there a way to convert seconds to time with DatePipe without shifted timezone?
Or is there any other pipe by angular which can be used for this purpose?

解决方案

So this quite tricky problem has a quite simple solution.
We just have to use 'H' instead of 'h' (range of which is 0-23).
Example:

<label>{{ 5 * 1000 | date:'H:mm:ss':'UTC'}}</label>

The reason of getting '12' was the usage of 'h' in time format which limits values from 1 to 12 and converts 00:00 to 12:00 (AM).

这篇关于Angular DatePipe-将秒转换为时区为零的时间(12而不是00)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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