Angular 比较并更改日期格式,如今天、昨天、,

Angular 比较并更改日期格式,如今天、昨天、,,angular,typescript,Angular,Typescript,我有以下数据: { "content": [ { "id": 15076772, "date": "2019-06-26T15:37:36" }, { "id": 15074042, "date": "2019-03-05T15:06:57" }, { "id": 15073812, "date": "2

我有以下数据:

     {
       "content": [
       {
         "id": 15076772,
         "date": "2019-06-26T15:37:36"
       },
       {
        "id": 15074042,
        "date": "2019-03-05T15:06:57"
       },
       {
        "id": 15073812,
        "date": "2019-09-17T14:32:18"
       },
       {
        "id": 15073810,
        "date": "2019-09-18T14:31:56"
       }
     ]
   }
我想根据当天的日期将每个日期转换为特定的格式

如果newDate()=2019-09-18T14:31:56,我们将在今天发布 如果newDate()不等于当天的日期,但等于昨天的日期,则将在昨天显示

然后,在一周中的几天之后(取决于日期),在离开一周之后,是数据的正常日期。 有点像聊天系统中显示的日期

我不知道angular是否能自动完成

我不知道angular是否可以自动完成,我知道Moment.js可以使用日历时间完成


如果有人能教我,那就太好了。

Angular没有内置的管道,但你可以自己建造管道或使用现有的管道()

不是我开发的,而是一个很好的例子:

如果你不介意它随着时间的推移而更新,你可以让它变得纯粹,并且只对你的情况保持简单(今天、昨天等等)

@管道({
姓名:'timeAgo',
纯:假
})
导出类TimeAgoPipe实现PipeTransform、OnDestroy{
私人定时器:数字;
构造函数(私有changeDetectorRef:changeDetectorRef,私有ngZone:ngZone){}
转换(值:字符串){
这个。removeTimer();
设d=新日期(值);
让我们现在=新日期();
让seconds=Math.round(Math.abs((now.getTime()-d.getTime())/1000));
让timeToUpdate=(Number.isNaN(秒))?1000:this.getSecondsUntilUpdate(秒)*1000;
this.timer=this.ngZone.runOutsideAngular(()=>{
如果(窗口类型!==“未定义”){
返回窗口。设置超时(()=>{
this.ngZone.run(()=>this.changeDetectorRef.markForCheck());
},timeToUpdate);
}
返回null;
});
让分钟=数学圆(数学绝对值(秒/60));
让小时=数学圆(数学绝对值(分钟/60));
让天=数学轮(数学abs(小时/24));
让月=数学圆(数学绝对值(天/30.416));
让年份=Math.round(Math.abs(days/365));
if(数字isNaN(秒)){
返回“”;

}否则,如果(seconds@andreetãtar),我认为这是一个很好的起点,尽管我肯定要修改它
@Pipe({
    name:'timeAgo',
    pure:false
})
export class TimeAgoPipe implements PipeTransform, OnDestroy {
    private timer: number;
    constructor(private changeDetectorRef: ChangeDetectorRef, private ngZone: NgZone) {}
    transform(value:string) {
        this.removeTimer();
        let d = new Date(value);
        let now = new Date();
        let seconds = Math.round(Math.abs((now.getTime() - d.getTime())/1000));
        let timeToUpdate = (Number.isNaN(seconds)) ? 1000 : this.getSecondsUntilUpdate(seconds) *1000;
        this.timer = this.ngZone.runOutsideAngular(() => {
            if (typeof window !== 'undefined') {
                return window.setTimeout(() => {
                    this.ngZone.run(() => this.changeDetectorRef.markForCheck());
                }, timeToUpdate);
            }
            return null;
        });
        let minutes = Math.round(Math.abs(seconds / 60));
        let hours = Math.round(Math.abs(minutes / 60));
        let days = Math.round(Math.abs(hours / 24));
        let months = Math.round(Math.abs(days/30.416));
        let years = Math.round(Math.abs(days/365));
        if (Number.isNaN(seconds)){
            return '';
        } else if (seconds <= 45) {
            return 'a few seconds ago';
        } else if (seconds <= 90) {
            return 'a minute ago';
        } else if (minutes <= 45) {
            return minutes + ' minutes ago';
        } else if (minutes <= 90) {
            return 'an hour ago';
        } else if (hours <= 22) {
            return hours + ' hours ago';
        } else if (hours <= 36) {
            return 'a day ago';
        } else if (days <= 25) {
            return days + ' days ago';
        } else if (days <= 45) {
            return 'a month ago';
        } else if (days <= 345) {
            return months + ' months ago';
        } else if (days <= 545) {
            return 'a year ago';
        } else { // (days > 545)
            return years + ' years ago';
        }
    }
    ngOnDestroy(): void {
        this.removeTimer();
    }
    private removeTimer() {
        if (this.timer) {
            window.clearTimeout(this.timer);
            this.timer = null;
        }
    }
    private getSecondsUntilUpdate(seconds:number) {
        let min = 60;
        let hr = min * 60;
        let day = hr * 24;
        if (seconds < min) { // less than 1 min, update every 2 secs
            return 2;
        } else if (seconds < hr) { // less than an hour, update every 30 secs
            return 30;
        } else if (seconds < day) { // less then a day, update every 5 mins
            return 300;
        } else { // update every hour
            return 3600;
        }
    }
}