如何与有角度的烟斗比较日期?

时间:2018-07-07 19:45:04

标签: angular angular6 angular-pipe

为什么此过滤器不起作用?

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

@Pipe({
  name: 'inProcess'
})
export class InProcessPipe implements PipeTransform {

  transform(events: any): any {
    if (!events) return events
    return events.filter(function (event) {
      return event.dateStart < Date.now();
    })
  }

}

这是我尝试使用Pipe的地方,我想带所有比Date.now()晚的 Events ,属性dateStart是一个String值在我的后端使用express和mongoose,但是从前端另存为Date,那么我该怎么做?

   <h2 class="text-left text-muted title-margin-top title-style">Late</h2>
    <div *ngFor="let event of events | inProcess">
      <div *ngIf="event.isCompleted === true">
        <div class="card">
      <div class="row">
        <div class="container">
          <div class="single-event col-md-10 col-md-offset-1">
            <div class="event-name">
              <a [routerLink]="['/event', event._id]">
                <h3>{{event.name}}</h3>
              </a>
            </div>
            <p>
              <span class="glyphicon glyphicon-file"></span>
              obra: {{ event.section.name}}
            </p>
            <p>
              <span class="glyphicon glyphicon-time"></span>
              publicado en: {{ event.created_at | date: 'dd / MM / yyyy' }}
            </p>

            <hr>

            <div class="row">
              <div class="container">
                <div class="col-lg-5 col-md-5 col-sm-5 col-xs-5">
                  <p class="card-text CardInfo text-center">Fecha Inicio</p>
                  <div class="text-center">
                    {{ event.dateStart | date: 'dd-MM-yyyy' }}
                    <hr class="style1">
                  </div>
                </div>

1 个答案:

答案 0 :(得分:2)

您拥有的比较将始终返回false。像这样比较时间戳之前,请先尝试解析字符串:

  transform(events: any): any {
   if (!events) return events
    return events.filter(function (event) {
     return Date.parse(event.dateStart) < Date.now();
   })
 }