I am working on a firebase project and for displaying time regarding and entity, what i got is the timestamp. I am working on a react native project. How to convert the received timestamp to properly formatted date. This is the timestamp:
I have this time available in this.props.time
, and so i tried this.props.time.toDate()
but what i got was an error.
Invariant Violation:Objects are not valid as a React child(found: Tue Nov 26 2019 11:51:58 GMT+05:30(Indian Standard Time))
When you view data using Firebase console, the Date/Timestamp is converted to your browser’s local timezone (e.g. 28 February 2019 at 15:53:59 UTC+8) You can use Java Date or Firebase Timestamp. I like to use Java Time or ThreeTenABP, so I shall use LocalDateTime to create a date (then convert them to Date or Timestamp ).
String formatTimestamp (int timestamp) { var format = new DateFormat ('d MMM, hh:mm a'); var date = new DateTime.fromMillisecondsSinceEpoch (timestamp * 1000); return format.format (date); } When we push the DateTime object to Firestore, it internally converts it to its own timestamp object and stores it.
To convert a Firestore date or timestamp to a JavaScript Date, we use firebase.firestore.Timestamp.fromDate to convert the a date to a Firestore timestamp. We call toDate on the timestamp object to convert it back to a JavaScript date.
There is no convenient Timestamp method to work with Java Time or threetenbp or ThreeTenABP. When you view data using Firebase console, the Date/Timestamp is converted to your browser’s local timezone (e.g. 28 February 2019 at 15:53:59 UTC+8) You can use Java Date or Firebase Timestamp.
You can create a Date object with new Date(time.seconds * 1000 + time.nanoseconds/1000000)
then manipulate it the way you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With