Skip to content

Instantly share code, notes, and snippets.

@Artawower
Created January 20, 2022 08:57
Show Gist options
  • Save Artawower/4f1699a84298b75373a10365f29f2acf to your computer and use it in GitHub Desktop.
Save Artawower/4f1699a84298b75373a10365f29f2acf to your computer and use it in GitHub Desktop.
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