10

What would you recommend to store dates using Firebase between a datetime and a timestamp, and why?

NSCoder
  • 1,594
  • 5
  • 25
  • 47

1 Answers1

21

Use a timestamp as this is locale-agnostic and does not require formatting and parsing agreements between clients. It's also a bit shorter.

Furthermore, utilize Firebase.ServerValue.TIMESTAMP, rather than trusting the clients to have their clocks set correctly. If, for example, a client's clock is off by 5 minutes and you utilize the client timestamps in a chat conversation, then rendering back the messages, the time shown would appear to be 5 minutes in the future for other users.

Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132
Kato
  • 40,352
  • 6
  • 119
  • 149
  • Thanks @Kato, spot on. I was actually getting the date from users using new Date(); and saving it to firebase which resulted in posts appearing to be from the future. – NSCoder Jun 14 '14 at 16:41
  • New docs here for TIMESTAMP: https://firebase.google.com/docs/reference/js/firebase.database.ServerValue#.TIMESTAMP – Michael Giovanni Pumo Jan 05 '17 at 16:23
  • @Kato re: " Firebase.ServerValue.TIMESTAMP". If i'm using the REST API directly, how would I generate the timestamp? – Learner Apr 29 '17 at 02:39