0

I need to store the date that something was created in the firebase database for a friend request, how can i store a date in firebase that i can work out how long ago since this date something happened. I would just store an NSDate in there but then what about timezones?

I dunno if firebase has something like Parse had where it would store date created and timezone automatically so you could read it from the server?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Eli
  • 668
  • 2
  • 13
  • 37
  • The Firebase Database stores JSON. Since NSDate is not a valid JSON type, you'll have to convert it to something that the database *can* store. The most common way is to store time as a so-called timestamp. See http://stackoverflow.com/questions/2997062/how-to-convert-nsdate-into-unix-timestamp-iphone-sdk – Frank van Puffelen Jul 03 '16 at 15:30

1 Answers1

1

You need to set it up by yourself ! I use this code to do the timestamp. Hope this is what u want.

 let formatter = NSDateFormatter()
    formatter.dateFormat = "yyyy/MM/dd, H:mm:ss"
    let defaultTimeZoneStr = formatter.stringFromDate(NSDate())

let post = ["\(key)":["username": username, "userPhoto": userPhotoUrl, "postPhoto": downLoadURL,"postText": postText!,"User":FIRAuth.auth()!.currentUser!.uid, "time":defaultTimeZoneStr] ]

                self.DatabaseRef.child("posts").updateChildValues(post)
Johnny Hsieh
  • 856
  • 2
  • 10
  • 23