Created
January 20, 2022 08:57
-
-
Save Artawower/4f1699a84298b75373a10365f29f2acf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Pipe, PipeTransform } from '@angular/core'; | |
@Pipe({ | |
name: 'dateformat', | |
}) | |
export class DateFormatPipe implements PipeTransform { | |
transform(value: string): string { | |
const valDate = new Date(value); | |
if (this.isToday(valDate)) { | |
return `Сегодня`; | |
} | |
if (this.isYesterday(valDate)) { | |
return `Вчера`; | |
} | |
return `${this.startFromZero(valDate.getDate())}.${this.startFromZero( | |
valDate.getMonth() + 1 | |
)}.${valDate.getFullYear()}`; | |
} | |
get now(): Date { | |
return new Date(); | |
} | |
startFromZero(val: number): string { | |
return `${val}`.length > 1 ? `${val}` : `0${val}`; | |
} | |
isToday(d: Date): boolean { | |
return ( | |
d.getDate() === this.now.getDate() && | |
d.getMonth() === this.now.getMonth() && | |
d.getFullYear() === this.now.getFullYear() | |
); | |
} | |
isYesterday(d: Date): boolean { | |
const yesterday = new Date(this.now); | |
yesterday.setDate(yesterday.getDate() - 1); | |
return ( | |
d.getDate() === yesterday.getDate() && | |
d.getMonth() === yesterday.getMonth() && | |
d.getFullYear() === yesterday.getFullYear() | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment