1
votes

Hi guys I'm trying to retrieve time and date stored in firebase but I'm returning the time value in milliseconds. Can someone please help me solve this. enter image description here

This is my code

class _UserListState extends State<UserList> {
  final databaseReference = Firestore.instance;

  getList() async {
    databaseReference
        .collection("bookings")
        .getDocuments()
        .then((QuerySnapshot snapshot) {
      return snapshot.documents;
    });
  }

  @override
  Widget build(BuildContext context) {
    return 
      Scaffold(
        appBar: AppBar(title: Text(widget.isDelete? "Delete User": "User List"),backgroundColor: Colors.indigo,),
        body: ListView(
          padding: EdgeInsets.all(12.0),
          children: <Widget>[
            SizedBox(height: 20.0),
            StreamBuilder<QuerySnapshot>(
                stream: databaseReference.collection('bookings').snapshots(),
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    return Column(
                      children: snapshot.data.documents.map((doc) {
                        print(doc.data);
                        return ListTile(
                          title:  RichText(
                            text: TextSpan(children: [
                              TextSpan(text: "Boat_id : ${doc.data["boat_id"]}\n"),
                              TextSpan(text: "Grand Total : ${doc.data["grand_total"]}\n"),
                              TextSpan(text: "Number of Passengers : ${doc.data["no_of_passengers"]}\n"),
                              TextSpan(text: "Promo code : ${doc.data["promo_code"]}\n"),
                              TextSpan(text: "Date and Time(From) : ${doc.data["selected_from_date"]}\n"),
                              TextSpan(text: "Date and Time(To) : ${doc.data["selected_to_date"]}\n"),
], style: TextStyle(color: Colors.black)),

enter image description here

1
kindly post formate of date which come from firestore - Jay Gadariya
i have attached a screenshot to my question please check - Rohit
What is the problem? - Peter Haddad
i want to retrive the date and time as it is not in milliseconds - Rohit
now you are retrieving timestamp, please post your firestore database, what is the format that you want for the time?! - Peter Haddad

1 Answers

3
votes

Change this:

TextSpan(text: "Date and Time(From) : ${doc.data["selected_from_date"]}\n"),
TextSpan(text: "Date and Time(To) : ${doc.data["selected_to_date"]}\n"),

into this:

TextSpan(text: "Date and Time(From) : ${doc.data["selected_from_date"].toDate()}\n"),
TextSpan(text: "Date and Time(To) : ${doc.data["selected_to_date"].toDate()}\n"),

The toDate() method should convert timeStamp into a normal date, you can find the method here:

https://github.com/flutter/plugins/blob/6cd8c677a21f1da0e78b20186be3ba0b0de08cef/packages/cloud_firestore/lib/src/timestamp.dart#L69