gpt4 book ai didi

angular - 比较和更改日期格式,如今天、昨天、

转载 作者:行者123 更新时间:2023-12-04 13:18:01 32 4
gpt4 key购买 nike

我有以下数据:

     {
"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 没有内置此功能,但您可以构建自己的管道或使用现有的管道 (https://github.com/AndrewPoyntz/time-ago-pipe)

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

https://github.com/AndrewPoyntz/time-ago-pipe/blob/master/time-ago.pipe.ts

如果您不介意它不会随着时间的流逝而更新,您可以让它变得纯粹并只针对您的情况(今天、昨天等)保持简单。

@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;
}
}
}

关于angular - 比较和更改日期格式,如今天、昨天、,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57993462/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com