Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print Firestore timestamp as formatted date and time

My timestamp returns Timestamp(seconds=1560523991, nanoseconds=286000000) in a Flutter Firestore snapshot.

I want to print it as properly formatted date and time.

I'm using DateTime.now() to store current DateTime in Firestore while creating new records and retrieving it using Firestore snapshot but I am not able to convert to into formatted date time. I'm using lib intl.dart for formatting.

Code for saving data

       d={'amount':amount,       'desc':desc,       'user_id':user_id,       'flie_ref':url.toString(),       'date':'${user_id}${DateTime.now().day}-${DateTime.now().month}-${DateTime.now().year}',       'timestamp':DateTime.now() return Firestore.instance.collection('/data').add(d).then((v){return true;     }).catchError((onError)=>print(onError));     });  Accessing with       FutureBuilder(                   future: Firestore.instance                       .collection('data')                       .where('user_id', isEqualTo:_user_id)                       .getDocuments(),                   builder: (BuildContext context,                       AsyncSnapshot<QuerySnapshot> snapshot) {                     if (!snapshot.hasData)                       return Container(                           child: Center(child: CircularProgressIndicator()));                     return ListView.builder(                         itemCount: snapshot.data.documents.length,                         itemBuilder: (BuildContext context, int index) {                           return Column(                             children: <Widget>[        Text(DateFormat.yMMMd().add_jm().format(DateTime.parse(snapshot.data.documents[index].data['timestamp'].toString())]);   .... 

Error thrown is

Invalid date format.

I'm expecting output is: 'Jan 17, 2019, 2:19 PM'

like image 528
aryan singh Avatar asked Jun 17 '19 08:06

aryan singh


People also ask

How do I display firestore timestamp to 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.

What is the format of firestore timestamp?

firestore. Timestamp. A Timestamp represents a point in time independent of any time zone or calendar, represented as seconds and fractions of seconds at nanosecond resolution in UTC Epoch time. It is encoded using the Proleptic Gregorian Calendar which extends the Gregorian calendar backwards to year one.

How do I display firebase timestamp?

The proper way to attach a timestamp to a database update is to attach a placeholder value in your request. In the example below Firebase will replace the createdAt property with a timestamp: firebaseRef = firebase. database().

How do I convert timestamp to DateTime in darts?

To get date time from a given timestamp, we can use the DateTime. fromMillisecondsSinceEpoch or DateTime. fromMicrosecondsSinceEpoch constructor. We have to multiply the timestamp input by 1000 because DateTime.


2 Answers

When we push the DateTime object to Firestore, it internally converts it to it's own timestamp object and stores it.

Method to convert it back to Datetime after fetching timestamp from Firestore:

Firestore's timestamp contains a method called toDate() which can be converted to String and then that String can be passed to DateTime's parse method to convert back to DateTime

DateTime.parse(timestamp.toDate().toString()) 
like image 117
Sushil Maurya Avatar answered Sep 21 '22 11:09

Sushil Maurya


Here is way!

Firestore will return TimeStamp like Timestamp(seconds=1560523991, nanoseconds=286000000).

This can be parsed as

Timestamp t = document['timeFieldName']; DateTime d = t.toDate(); print(d.toString()); //2019-12-28 18:48:48.364 
like image 33
Bharath Avatar answered Sep 18 '22 11:09

Bharath