【问题标题】:Ionic Firebase Cloudfunctions TimestampIonic Firebase Cloudfunctions 时间戳
【发布时间】:2020-11-04 15:07:55
【问题描述】:

感谢您的帮助。 (更新问题以进行澄清)

1.- 在 ioinic(角度)HTML 中,我要求输入:“开始”。 html 表单类型是时间戳,所以在 html 中我可以“以图形方式”选择一个日期。

表格是这样的:

<ion-label>Start</ion-label>
  <ion-datetime formControlName="start" type="timestamp" 
    displayFormat="DMMMYYYY" min="2019" max="2070"></ion-datetime>
</ion-item>

2.- 然后我将其作为新日期(输入值)存储在 firestore 中

example--- {start: new Date(start)} 

在 Firestore 中保存为时间戳:7 月 15 日 01:23:00 UTC 5

作为记录,如果我保存为

{start: start}

它在 Firestore 中保存为:2020-07-15T01:23:00-05:00

,但是我仍然无法在 html 中显示回来……答案可以在这里吗?

3.- 然后,如果我获取数据并通过 html 订阅: 示例:(使用第一个显示的格式)

let d of (data| async).....{{d.start.toDate()|date: "dd/MMM/yy"}}

在 html 中显示我想要的日期...15/jul/2020

4.- 如果我只是将相同的 d.start 传递给另一个函数并直接保存到 Firestore,它仍然会像保存在另一个文档中一样保存。

5.- 问题是我将此数据作为数据的一部分传递给可调用的云函数,然后,已经在我检索的 CF 函数中:

const start = data.start;

6.- 这是问题所在,无论我如何尝试,它都不会像在其他 firestore 文档中那样存储到另一个 firebase 文档。

试过了:

const start = new Date(data.start)
const start = new Date(data.start.nanoseconds)
const start = new Date(data.starts seconds)
const start = data.start
const start = data.start.nanoseconds.
etc... many combinations truly, read, searched, googled and can't get it.

有时它存储 NaN,有时只是几秒/纳秒,有时返回 500 无效日期格式,有时按我的意愿存储,但使用 1970 年的日期(这是最接近的日期)

我正在尝试仅在 Firestore Cloud Function 中进行转换。

感谢您的帮助。

【问题讨论】:

  • 虽然你需要添加一些代码,因为仅仅列出你所拥有的不会让任何人知道你的问题可能出在哪里,但是 ionic 使用 ISO 时间,所以当你以你想要的格式保存数据到 firebase 时,因此当您在表单中重新填写数据时,日期会以您的格式而不是 iso 格式设置,因此您需要在 ts 文件中设置 vales,如声明 startDate:Date;然后在数据到达时使用 this.startDate = newDate(here put the value from firebase).toISOString();和结束日期一样,因此应该可以工作,或者您可以使用管道和地图来实现这一点。
  • 感谢 U @MostafaHarb,这很有用,我了解代码的需要,并会尽快发布,仍然感谢您的想法。
  • 谢谢@MostafaHarb 还没解决
  • 究竟还有什么问题?

标签: angular ionic-framework google-cloud-firestore google-cloud-functions


【解决方案1】:

Ionic 使用 ISO 时间,因此当您以所需格式将数据保存在 firebase 中时,因此当您在表单中重新填写数据时,日期会以您的格式而不是 iso 格式设置,因此您需要设置 vales在 ts 文件中就像声明一样

startDate:Date; 

然后在数据到达时使用

this.startDate = newDate(here put the value from firebase).toISOString(); 

与结束日期相同,因此应该可以工作,或者您可以使用管道和地图来实现这一点。

更新部分

您可以在数据库 yyyy-MM-ddT00:00:00.000z 中以 iso 格式正常保存时间,因此当从 firebase 获取数据时 正如我在粉丝之前告诉你的那样,要么使用管道和地图来解决它,要么通过这种方式:

const splitedDate = firebaseDate.split('T');
const ymd = splitedDate[0];
// now ymd is yyyy-MM-dd
const time = splitedDate[1].split('.');
// now time is 00:00:00
const customDate = this.formatToDate(ymd.split('-'), time);

formatToDate(dtime, time) {
    const date = new Date(dtime[0], (parseInt(dtime[1]) - 1), dtime[2]); 
    const month = date.toLocaleString('default', { month: 'short' }); 
    return `${dtime[2]} of ${month} at ${time}`;
}

【讨论】:

  • 伙计,据我所知,现在我正在保存 7 月 15 日 01:23:00 UTC 5,但您的建议是保存:2020-07-15T01:23:00-05 :00,我知道这可以工作,但这带来了一个与 HTML 相关的不同问题,它现在的存储方式,我使用 let d of (data| async).....{{d.start. toDate()|date: "dd/MMM/yy"}} 在 html...15/jul/2020 中显示我想要的日期,虽然它是 2020-07-15T01:23:00-05:00可以为云功能工作,我无法到达那里,因为我卡在 HTML 中的显示中,那会怎么样?提前致谢
  • @Chucho7 你误解了我的意思,senario 是你在 fb 中以你想要的格式保存数据,当从 db 取回数据时,得到像 (new Date(res.date ).toISOString()) ,因此日期将更改为选择器时区,因为时区不采用 utc 值,它需要 iso 格式的时间,因此当再次将数据输入到 db 时,您将需要重新转换它的值是您在 UTC 中需要的格式。因此,为了更快地工作,您可以使用管道和映射进行此 iso 转换和 utc 转换,而不是使用 for 循环为每个对象设置此转换。
  • 我已经接近了,因为我更愿意直接在云函数中进行整个转换,方法是使用: const start= new Date(data.start.seconds *1000);我得到的是 2020-07-15T01:23:00.000Z ,所以这几乎是正确的!我只想将其转换为:7 月 15 日 01:23:00 UTC 5 存储在 Firestore 中时,您认为这可能吗?不弄乱整个离子设置?谢谢!
  • @Chucho7 是否始终是 UTC 5 的常数?或者它可能会根据用户时区更改为 utc 3 或 4?
  • 哦,谢谢,我不太关心 UTC,好吧,也许是因为缺乏知识,也许,或者我不明白这个问题,对不起,但有它存储在 Firestore 中作为时间戳:7 月 15 日 01:23:00 比将其存储为时间戳容易得多:2020-07-15T01:23:00.000Z
【解决方案2】:

终于解决了
const start= new Date(data.start.seconds *1000);不知道为什么console.log() 显示不同的格式,但是当将它存储到firestore 时,它​​会根据我的需要保存它,因为它已经通过ionic 存储在其他firestore 文档中。感谢那些帮助的人的时间和帮助! :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-09
    • 1970-01-01
    • 2020-12-02
    • 2021-11-23
    • 2016-12-13
    • 1970-01-01
    • 2020-04-08
    • 2020-05-11
    相关资源
    最近更新 更多