angular用DatePipe进行日期转换

5 篇文章 1 订阅

angular中日期转换为要显示的格式可以使用DatePipe:

(1)HTML 

<span *ngIf="CompareYear(notification.date) !== currentYear; else elseCurrentYear">

  {{ notification.date | date: 'y MMM d' }}

</span>

<ng-template #elseCurrentYear>

  <span *ngIf="CompareDate(notification.date) !== today; else elseToday">

    {{ notification.date | date: 'MMM d' }}

  </span>

</ng-template>

<ng-template #elseToday>

  <span>

    {{ notification.date | date: 'h:mm a' }}

  </span>

</ng-template>

 

(2)TS

import { DatePipe } from '@angular/common';

export class XXX{

today = this.datePipe.transform(Date.now(), 'yyyy-MM-dd');

currentYear = this.datePipe.transform(Date.now(), 'yyyy');



constructor(private readonly datePipe: DatePipe) {}



 CompareDate(date: any): any {

    return this.datePipe.transform(date, 'yyyy-MM-dd');

  }



  CompareYear(date: any): any {

    return this.datePipe.transform(date, 'yyyy');

  }

}

 

注:https://angular.io/api/common/DatePipe

format包括:short、medium、long、full、shortDate、mediumDate、longDate、fullDate、shortTime、mediumTime、longTime、fullTime;

时间的显示样式
类型形式例子
shortM/d/yy, h:mm a6/15/15, 9:03 AM
mediumMMM d, y, h:mm:ss aJun 15, 2015, 9:03:01 AM
longMMMM d, y, h:mm:ss a zJune 15, 2015 at 9:03:01 AM GMT+1
fullEEEE, MMMM d, y, h:mm:ss a zzzzMonday, June 15, 2015 at 9:03:01 AM GMT+01:00
shortDateM/d/yy6/15/15
mediumDateMMM d, yJun 15, 2015
longDateMMMM d, yJune 15, 2015
fullDateEEEE, MMMM d, yMonday, June 15, 2015
shortTimeh:mm a9:03 AM
mediumTimeh:mm:ss a9:03:01 AM
longTimeh:mm:ss a z9:03:01 AM GMT+1
fullTimeh:mm:ss a zzzz9:03:01 AM GMT+01:00

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Custom format options:

Field typeFormatDescriptionExample Value
EraG, GG & GGGAbbreviatedAD
 GGGGWideAnno Domini
 GGGGGNarrowA
YearyNumeric: minimum digits2, 20, 201, 2017, 20173
 yyNumeric: 2 digits + zero padded02, 20, 01, 17, 73
 yyyNumeric: 3 digits + zero padded002, 020, 201, 2017, 20173
 yyyyNumeric: 4 digits or more + zero padded0002, 0020, 0201, 2017, 20173
MonthMNumeric: 1 digit9, 12
 MMNumeric: 2 digits + zero padded09, 12
 MMMAbbreviatedSep
 MMMMWideSeptember
 MMMMMNarrowS
Month standaloneLNumeric: 1 digit9, 12
 LLNumeric: 2 digits + zero padded09, 12
 LLLAbbreviatedSep
 LLLLWideSeptember
 LLLLLNarrowS
Week of yearwNumeric: minimum digits1... 53
 wwNumeric: 2 digits + zero padded01... 53
Week of monthWNumeric: 1 digit1... 5
Day of monthdNumeric: minimum digits1
 ddNumeric: 2 digits + zero padded01
Week dayE, EE & EEEAbbreviatedTue
 EEEEWideTuesday
 EEEEENarrowT
 EEEEEEShortTu
Perioda, aa & aaaAbbreviatedam/pm or AM/PM
 aaaaWide (fallback to a when missing)ante meridiem/post meridiem
 aaaaaNarrowa/p
Period*B, BB & BBBAbbreviatedmid.
 BBBBWideam, pm, midnight, noon, morning, afternoon, evening, night
 BBBBBNarrowmd
Period standalone*b, bb & bbbAbbreviatedmid.
 bbbbWideam, pm, midnight, noon, morning, afternoon, evening, night
 bbbbbNarrowmd
Hour 1-12hNumeric: minimum digits1, 12
 hhNumeric: 2 digits + zero padded01, 12
Hour 0-23HNumeric: minimum digits0, 23
 HHNumeric: 2 digits + zero padded00, 23
MinutemNumeric: minimum digits8, 59
 mmNumeric: 2 digits + zero padded08, 59
SecondsNumeric: minimum digits0... 59
 ssNumeric: 2 digits + zero padded00... 59
Fractional secondsSNumeric: 1 digit0... 9
 SSNumeric: 2 digits + zero padded00... 99
 SSSNumeric: 3 digits + zero padded (= milliseconds)000... 999
Zonez, zz & zzzShort specific non location format (fallback to O)GMT-8
 zzzzLong specific non location format (fallback to OOOO)GMT-08:00
 Z, ZZ & ZZZISO8601 basic format-0800
 ZZZZLong localized GMT formatGMT-8:00
 ZZZZZISO8601 extended format + Z indicator for offset 0 (= XXXXX)-08:00
 O, OO & OOOShort localized GMT formatGMT-8
 OOOOLong localized GMT formatGMT-08:00
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值