Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase firestore timestamp to Formatted Date

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:

enter image description here

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))

like image 475
FortuneCookie Avatar asked Nov 26 '19 06:11

FortuneCookie


People also ask

How to convert date/timestamp to local time in Firebase?

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 ).

How to convert a string to a timestamp in FireStore?

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.

How do I convert a FireStore date to a JavaScript date?

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.

How do I use Java timestamp with Java time or threetenbp?

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.


1 Answers

You can create a Date object with new Date(time.seconds * 1000 + time.nanoseconds/1000000) then manipulate it the way you want.

like image 51
Olivier Lépine Avatar answered Sep 17 '22 16:09

Olivier Lépine