推送日期+时间时间戳Firestore

时间:2019-05-20 03:36:39

标签: angular google-cloud-firestore

我正在使用反应形式的Angular Material和Firestore存储日期和时间。由于Angular Material仅具有Datepicker(https://material.angular.io/components/datepicker/overview)而没有Timepicker,因此我在另一个字段中工作了时间,也就是说,我有两个字段。

但是当我想按日期和时间过滤数据时,这给我带来了其他问题。

我的问题是,如何将日期和时间存储在单个Firestore字段中?

目前这是我所拥有的:

components.ts

this.forma = fb.group ({
  fechaInicio: [ '', Validators.required ],
  horaInicio: [ '', Validators.required ],  
});

components.html

<div class="row">
  <mat-form-field class="col-lg-7 col-6" appearance="outline">
    <mat-label>Fecha inicio</mat-label>
    <input matInput [matDatepicker]="picker" placeholder="Fecha inicio" formControlName="fechaInicio">
    <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
    <mat-datepicker touchUi #picker></mat-datepicker>
  </mat-form-field>
  <mat-form-field class="col-lg-5 col-6" appearance="outline">
    <mat-label>Hora inicio</mat-label>
    <mat-select placeholder="Hora inicio" formControlName="horaInicio">
    <mat-option *ngFor="let hora of horas" [value]="hora">
      {{ hora }} hrs.
    </mat-option>
    </mat-select>
  </mat-form-field>
  </div>

Firestore中的结果是这样的: enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

任何您想称呼的都很好。然后,您可以使用orderByChild('created')。

设置时间时,我也大多使用firebase.database.ServerValue.TIMESTAMP

ref.child(key).set({
id: itemId,
content: itemContent,
user: uid,
created: firebase.database.ServerValue.TIMESTAMP 
})