Chucho7
Chucho7

Reputation: 75

Ionic Firebase Cloudfunctions Timestamp

thanks for help. (update the question for clarification)

1.- In ioinic (angular) HTML, I ask for an input: "start". The html form type is timestamp, so in html I'm able to "graphically" pick a date.

The form is this:

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

2.- Then I store this in firestore as new Date(value of the input)

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

Saves in Firestore as timestamp: 15 of july at 01:23:00 utc 5

For the record, if I save just as

{start: start}

it saves in Firestore as: 2020-07-15T01:23:00-05:00

, but this I'm still unable to display back in html...could the answer be here?

3.- Then, if I get the data and subscribe via html: Example: (using first shown format)

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

shows me date as I want in html...15/jul/2020

4.- If I just pass this same d.start to another function and save to Firestore directly it still saves just as it was saved on the other doc.

5.- the problem is that I pass this data to a callable Cloud Function, as part of data, then, already in the CF function I retrieve:

const start = data.start;

6.- Here is the problem, no matter how I have tried, it does not get stored to another firebase doc as it was in the other firestore docs.

have tried:

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.

Sometimes it stores NaN, sometimes just a number of seconds/nanoseconds, sometimes returns 500 invalid date format, sometimes stores as I would wish, but with a 1970's date (this is the closest one)

I'm trying to do the conversion just in the Firestore Cloud Function.

Thanks for the help.

Upvotes: 0

Views: 459

Answers (2)

Chucho7
Chucho7

Reputation: 75

It was finally solved by using
const start= new Date(data.start.seconds *1000); not sure why when console.log() shows a different format, but when storing it into firestore, it saved it just as I needed, as already stored in other firestore docs via ionic. Thanks for the time and help of those who helped! :)

Upvotes: 0

Mostafa Harb
Mostafa Harb

Reputation: 1675

Ionic uses ISO time, so when you save data in firebase in the format you want, thus when you refill your data in the form, the dates are set in your format and not in iso format so you need to set the vales in ts file as like declare

startDate:Date; 

Then on data arrive use

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

and same thing to the end date and thus should work or you could use pipe and map to achieve this.

Update Part

You can save the time normally in iso format in database yyyy-MM-ddT00:00:00.000z ,so when getting the data from the firebase As i told you before you fan either use pipe and map to solve it or by this way:

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}`;
}

Upvotes: 2

Related Questions