Sending ion-datetime as timestamp to Firebase

Hi! I’m currently developing a project using Ionic and Firebase, but I can’t figure out how to send the value I get from the ion-datetime as a timestamp to firebase. It just appears to be a string on firebase and I need it to be timestamp so I can apply order to my data.

This is my ion-datetime HTML code:

                                <ion-label>Time to pick up:</ion-label>
                                <ion-datetime displayFormat="HH:mm" hourValues="8,9,10,11,12,13,14,15,16,17,18,19,20,21,22" minuteValues="0,15,30,45" [(ngModel)]="pickup" name="pickup" required></ion-datetime>

In typescript I have this:

pickup: Date = new Date();

The value stored in pickup is the one that I send to firebase, but it appears as a string once I check on firebase.

TypeScript only works at compile time. Once things get to runtime, it’s powerless. Therefore, make pickup a string, and convert it to a Date separately before sending to Firebase.

1 Like

to store a Date in firebase as a Timestamp

firebase.firestore.Timestamp.fromDate(new Date())

1 Like

You can use this in ts…
here ‘c’ and ‘d’ are variable…

c = new Date
console.log© :>> Thu Jan 09 2020 11:31:23 GMT+0530 (India Standard Time)

If c is your date from ion-date and time… pass that value in following to get the timestamp…
d= Math.round(c/1000)
console.log(d):>>> 1578549684

Hope that this timestamp works out in firebase. Do let me know if it works out…

1 Like