简体   繁体   English

Angular2 - 日期管道问题

[英]Angular2 - Issue with Date pipe

I am trying to format the date time that I fetch from the DB in the following format: 我试图以下列格式格式化从数据库中提取的日期时间:

15/09/2016, 20:45 2016年9月15日,20:45

However using the default date Pipe provided by Angular2, I get the following ( When I run on a real device ) However, On the browser it shows the correct format: 但是,使用Angular2提供的默认date管道,我得到以下内容( 当我在真实设备上运行时但是,在浏览器上它显示正确的格式:

15/09/2016, 20:8:45 PM 2016年9月15日,20:8:45

Here is how I am formatting it in the HTML side: 这是我在HTML方面格式化它的方式:

{{ order.time | date:"dd/MM/y, HH:mm" }}

Any ideas on what's wrong? 关于什么是错的任何想法?

There are many ways to do it. 有很多方法可以做到这一点。 You have to make sure that date which you fetch must be in date format. 您必须确保您获取的日期必须采用日期格式。 If it is, it will work properly with date pipe . 如果是,它将与日期管道正常工作。 If it is not you have to make sure that you convert it in to date as shown below. 如果不是,您必须确保将其转换为日期,如下所示。 (This may help you to understand what I mean) (这可以帮助你理解我的意思)

Working Demo : https://plnkr.co/edit/aaFu5G759ghUQvk1a9Zw 工作演示: https//plnkr.co/edit/aaFu5G759ghUQvk1a9Zw

import { Component } from '@angular/core';
import { DomSanitizer, SafeResourceUrl, SafeUrl } from '@angular/platform-browser';
@Component({
  selector: 'my-app',
  template: `      
  {{date|date:"dd/MM/y, HH:mm"}} //<----- here make sure date variable has proper date(type)
  `
})
export class AppComponent { 

  date=new Date(2016,08,5,20,45);   

  //<---- date variable gets proper date(type)
  //<---- also note month starts with zero(0) so for sep month (08) is taken.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM