rameez khan
rameez khan

Reputation: 359

Flutter how to convert timestamp date time

I am getting date and time from store. In data base its look like this

enter image description here

Need to know how can I show this as DD/MM/YY

I am trying to do like this

    String timeString = snapshot.data[index]['lastupdate'].toString();
    DateTime date = DateTime.parse(timeString);
    print(DateFormat('yyyy-MM-dd').format(date));

Its showing error of Invalid date format

Upvotes: 2

Views: 9673

Answers (2)

HoRiz
HoRiz

Reputation: 787

Use function to get date from Timestamp like this:

readDate(Timestamp dateTime) {
    DateTime date = DateTime.parse(dateTime.toDate().toString());
    // add DateFormat What you want. Look at the below comment example 
    //String formatedDate = DateFormat('dd-MMM-yyy').format(date); 
    String formatedDate = DateFormat.yMMMMd().format(date);
    return formatedDate;
  }

Use the function when you want it. For example:

Text(readDOB(streamSnapshot.data!["dob"]))

For this you should install intl package. read

Upvotes: 1

Umaiz Khan
Umaiz Khan

Reputation: 1227

Try like this your lastupdate date is not convertime inDate thats why its showing error

DateTime date = DateTime.parse(snapshot.data[index]['lastupdate'].toDate().toString());
print(DateFormat('dd-MMM-yyy').format(date));

Upvotes: 5

Related Questions