2

i have a small problem with firebase and angular 2.

I try to save a user into the firebase database. Everythings works good, but the date isnt stored.

let now: Date = new Date();
let premiumExpireDate: Date = new Date();
premiumExpireDate.setDate(premiumExpireDate.getDate()+14);

// Insert data on our database using AngularFire.
this.angularfire.database.object('/accounts/' + userId).set({
    name: name, // works well
    dateUpdated: now, // isnt stored
    premiumExpireDate: premiumExpireDate // isnt stored too
}).then(() => {
    ...
});

Do you have a idea, how to save and also restore a data to and from firebase?

Thanks a lot!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Fargho
  • 1,267
  • 4
  • 15
  • 30
  • The Firebase Database stores JSON data and `Date` is not a JSON type. See http://stackoverflow.com/questions/40829336/storing-date-input-values-on-firebase for options. – Frank van Puffelen Feb 16 '17 at 17:55

1 Answers1

5

You can't save a date object. You'll have to convert it to timestamp

this.angularfire.database.object('/accounts/' + userId).set({
    name: name, // works well
    dateUpdated: now.getTime(), // isnt stored
    premiumExpireDate: premiumExpireDate.getTime() // isnt stored too
}).then(() => {
    ...
});
Mathew Berg
  • 28,625
  • 11
  • 69
  • 90
  • Ah, thats easy. Whats the best way to convert this back to a Date? Maybe new Date(timestamp)? Or something else? Thanks a lot! – Fargho Feb 16 '17 at 19:26