import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'dateTransform'
})
export class DateTransformPipe implements PipeTransform {

  transform(value: any, ...args: any[]): any {
    let fullDate = new Date(value);//传入的日期
    let year = fullDate.getFullYear();
    let month = (fullDate.getMonth() + 1).toString().padStart(2, '0');
    let date = fullDate.getDate().toString().padStart(2, '0');
    let hour = fullDate.getHours().toString().padStart(2, '0');
    let minute = fullDate.getMinutes().toString().padStart(2, '0');
    let nowFullDate = new Date();//当前的日期
    let nowYear = nowFullDate.getFullYear();//当前的年份
    let nowDate = nowFullDate.getDate().toString().padStart(2, '0');//当前是几号
    let day1 = new Date(nowFullDate.getTime() - 24 * 60 * 60);//昨天
    let day1Date = day1.getDate().toString().padStart(2, '0');//昨天是几号
    if (year !== nowYear) {//跨年
      return `${year}-${month}-${date} ${hour}:${minute}`;
    } else if (day1Date === date) {//昨日
      return `昨日 ${month}-${date} ${hour}:${minute}`;
    } else if (nowDate === date) {//今日
      return `今日 ${month}-${date} ${hour}:${minute}`;
    } else {
      return `${month}-${date} ${hour}:${minute}`;
    }
  }

}
