39 lines
1.1 KiB
TypeScript
Raw Normal View History

import { Pipe, PipeTransform } from '@angular/core';
import { StringUtil } from '../utils/string.util';
@Pipe({
name: 'dateToStringChatList'
})
export class DateToStringForChatRoomListPipe implements PipeTransform {
transform(value: any): string {
const curDate = new Date();
const yesterDate = new Date(curDate.getTime() - 1 * 24 * 60 * 60 * 1000);
let date: Date;
if (typeof value === 'string') {
date = new Date(value.toString());
} else if (value instanceof Date) {
date = value;
} else {
return value;
}
if (
curDate.getFullYear() === date.getFullYear() &&
curDate.getMonth() === date.getMonth() &&
curDate.getDate() === date.getDate()
) {
// 당일
return StringUtil.dateFormat(date, 'a/p HH:mm');
} else if (
yesterDate.getFullYear() === date.getFullYear() &&
yesterDate.getMonth() === date.getMonth() &&
yesterDate.getDate() === date.getDate()
) {
// 어제
return '어제';
} else {
return StringUtil.dateFormat(date, 'MM.dd');
}
}
}