【问题标题】:Format Ionic-Datetime to Firebase Timestamp将离子日期时间格式化为 Firebase 时间戳
【发布时间】:2021-04-14 03:38:12
【问题描述】:

我想在 firbase 中保存用户选择的日期时间(使用 ion-datetime)作为时间戳。 现在,不幸的是,firebase 将日期保存为字符串。

采摘日期和时间:

<ion-item>
      <ion-label>Fälligkeits Um</ion-label>
      <ion-datetime [(ngModel)]="timestamp" displayFormat="DD.MM.YYYY HH:mm" placeholder="Select Date"></ion-datetime>
    </ion-item>

初始化:

export class CreateToDoPage implements OnInit {

  titel = ""
  label = ""
  timestamp = ""
  constructor(public afAuth: AngularFireAuth, private router: Router, public afStore: AngularFirestore) { }

  ngOnInit() {
  }

保存到 Firebase:

async createToDo() {
    const { titel, label, timestamp } = this;
    try {
      const user = await this.afAuth.currentUser;
      await this.afStore.collection("/user/" + user.uid + "/todos").add({ name: titel, label: label, due: timestamp }).then((f) => { console.log(f) }).catch((error) => { console.dir(error) });
    } catch (error) {
      console.dir(error);
    }
  }

我已经试过设置变量timestamp的类型了:

timestamp: AngularFirestore.timestamp = ""

但是我得到了这个错误:

[ts] 'AngularFirestore' 仅指一种类型,但被用作 命名空间在这里。

如果您有任何想法,我将不胜感激:)

【问题讨论】:

    标签: angular firebase datetime ionic-framework google-cloud-firestore


    【解决方案1】:

    &lt;ion-datetime&gt; 组件在 ngModel 数据上输出一个字符串,使用 ISO8601 格式。

    要将其传递给 firebase,只需将其转换为 Date 对象,如下所示:

    async createToDo() {
        const { titel, label, timestamp } = this;
    
        try {
          const parsedDate = new Date(timestamp)
          const user = await this.afAuth.currentUser;
          await this.afStore.collection("/user/" + user.uid + "/todos").add({ name: titel, label: label, due: parsedDate }).then((f) => { console.log(f) }).catch((error) => { console.dir(error) });
        } catch (error) {
          console.dir(error);
        }
      }
    

    【讨论】:

      猜你喜欢
      • 2020-03-21
      • 2018-06-15
      • 2015-08-09
      • 1970-01-01
      • 2016-11-26
      • 2020-10-23
      • 2020-02-24
      • 2010-09-09
      • 2019-02-25
      相关资源
      最近更新 更多