Ionic Firebase Cloudfunctions Timestamp



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

1.-在ioinic(angular(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中显示我想要的日期。。。2020年7月15日

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函数中进行转换。

谢谢你的帮助。

Ionic使用ISO时间,因此当您以所需格式在firebase中保存数据时,因此当在表单中重新填充数据时,日期将以您的格式设置,而不是以ISO格式设置,因此您需要将ts文件中的vales设置为类似于声明

startDate:Date; 

然后在数据到达时使用

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

同样的事情到结束日期,因此应该工作,或者你可以使用管道和地图来实现这一点。

更新部件

您可以在数据库yyyy-MM-ddTT00: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}`;
}

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

最新更新