如何将当前日期时间与某个数据库时间进行比较

时间:2016-12-22 07:25:54

标签: javascript angular typescript

我有一个数据库表,其值为:

 "295fc51f6b02d01d54a808938df736ed" : {
    "author" : "James Iva",
    "authorID" : "JBvLC3tCYCgFeIpKjGtSwBJ2scu1",
    "geometry" : {
      "latitude" : 29.4241219,
      "longitude" : -98.49362819999999
    },
    "listDate" : 1482331706209,
    "openHours" : {
      "Friday" : {
        "closeTime" : "17:00",
        "openTime" : "09:00",
        "status" : true
      },
      "Monday" : {
        "closeTime" : "17:08",
        "openTime" : "09:00",
        "status" : true
      },
      "Saturday" : {
        "closeTime" : "17:00",
        "openTime" : "09:00",
        "status" : true
      },
      "Sunday" : {
        "closeTime" : "17:00",
        "openTime" : "10:00",
        "status" : true
      },
      "Thursday" : {
        "closeTime" : "17:00",
        "openTime" : "09:21",
        "status" : true
      },
      "Tuesday" : {
        "closeTime" : "17:00",
        "openTime" : "04:00",
        "status" : false
      },
      "Wednesday" : {
        "closeTime" : "17:00",
        "openTime" : "10:00",
        "status" : false
      }
    },
    "pPhone" : "no_phone",
    "placeAdd" : "San Antonio, TX, USA",
    "placeCat" : "Education"
  }
}

我要做的是检查当前日期时该地点是否已打开(状态为真),并在模板中显示以下内容:

IT'S THURSDAY 4:12PM - WE'RE OPEN!

我怎样才能在angular2中实现这个目标?

以下是我从服务中获取数据的订阅代码:

ngOnInit() {
    this.sub = this.route.params.subscribe(params => {
        this.id = params['id'];
        let str = params['string'];
       // Retrieve Pet with Id route param
       this._placesService.findPetById(this.id).subscribe(place => {
         this.place = place; //Here should be the opening data!

       });
});
}

1 个答案:

答案 0 :(得分:0)

要了解如何完成这项工作,请看一下:

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>Hello {{name}}</h2>
      <br />
      {{ opening }}
    </div>
  `,
})
export class App {
  name:string;
  opening: string;

  constructor() {
    this.name = 'Angular2'

    this._refresh();
  }

  private _getDayName(): string {
    return new Date().toLocaleString("en-us", { weekday: 'long' });
  }

  // converts string-times into Date and checks if its opened ..
  private _isOpen(todaysOpeningData: any): bool {
    const curDate = new Date();
    const open = new Date();
    const close = new Date();

    // set opening time
    open.setHours(+todaysOpeningData.openTime.split(':')[0]);
    open.setMinutes(+todaysOpeningData.openTime.split(':')[1]);

    // set closing time
    close.setHours(+todaysOpeningData.closeTime.split(':')[0]);
    close.setMinutes(+todaysOpeningData.closeTime.split(':')[1]);

    // check if its opened !
    return curDate >= open && curDate <= close;
  }

  // check and build string ..
  private _checkOpeningHours(data: any): string {
    const curDayName = this._getDayName(); // get current day name
    const todaysOpeningData = openingdata.openHours[curDayName]; // get opening time for today with current day name

    if (!todaysOpeningData) return "ERROR!"; // there are no opening-times for today?
    if (!todaysOpeningData.status) return `IT'S ${curDayName.toUpperCase()} - WE ARE CLOSED TODAY!`; // we are closed entirely today !

    return `IT'S ${curDayName.toUpperCase()}, ${new Date().toLocaleString("en-US", { hour: '2-digit', minute: '2-digit' })} - ${this._isOpen(todaysOpeningData) ? 'WE ARE OPEN' : 'SORRY, WE ARE CLOSED'}!`;
  }

  private _refresh() {
    this.opening = this._checkOpeningHours(openingdata.openHours[this._getDayName()]);
    console.log(this.opening);

    setTimeout(() => this._refresh(), 60 * 1000);
  }
}

live-demo:https://plnkr.co/edit/R5fk7eJrltGx2zOuVhuz?p=preview

<强>更新

您应该像这样使用它:

this.sub = this.route.params.subscribe(params => {
   this.id = params['id'];
   let str = params['string'];

   // Retrieve Pet with Id route param
   this._placesService
      .findPetById(this.id)
      .subscribe(place => {
         this.place = place; //Here should be the opening data!

         // assuming that 'this.id' is '295fc51f6b02d01d54a808938df736ed'
         this.yourTargetVariable = this._checkOpeningHours(place[this.id]);
   });
});