typescript 我如何过滤数据,根据日期,现在在Angular 打字?

ogq8wdun  于 2022-12-24  发布在  TypeScript
关注(0)|答案(3)|浏览(112)

我有预订,我想获取所有已完成且已通过的预订(〈date now的预订)
我试着根据日期按结帐属性过滤预订,但没有用,它总是显示我所有的预订列表。
这是我的Reservation模型:

export class Reservation {
  id!: number;
  nb!: number;
  montantTotal!: number;
  user!: User;
  extra!: number;
  couponon!: number;
  nbrooms!: number;
  montantExtraRoom!: number
  dateCreated!: Date
  vol!: Vol;
  checkin!: Date;
  checkout!: Date;
  persones: Persone[] = [{
    titre:'',
    firstname:'',
    lastname:'',
    datebirth:'',
    nationality:'',
    passport:0,
    country:'',
    passworddateout:'',
  }]
}

这是我的组件:

export class UserMybookingComponent implements OnInit {
  myres!: Reservation[]
  myDate = new Date();

  constructor(private reservationService: ReservationService, private datePipe: DatePipe ) { 

  }
  
  ngOnInit() {
    this.mybooking()
   
  }

  mybooking(){
    
    this.reservationService.mybook().subscribe(
      data => {this.myres=data;
        this.myres.filter(res => new Date(res.checkout) > this.myDate)
      }
      
    )
  }
}
dfty9e19

dfty9e192#

如果要显示数据,请尝试以下操作

displayDate: any;

mybooking(){

this.reservationService.mybook().subscribe(
  data => {this.myres=data;
    this.myres.filter(res => new Date(res.checkout) > this.myDate)

 this.displayDate = res;
  }
  
)
}

把这放进你的html文件

{{displayDate?."index of the element you want to display goes here" | date:'fullDate'}}
bzzcjhmw

bzzcjhmw3#

请始终使用getTime()或valueOf()来准确比较日期(毫秒)

new Date(res.checkout).getTime() > this.myDate.getTime()

相关问题