How can I get only date and time value from timestamp value in flutter

Viewed 1737

I am developing a Flutter project and I want to get 'timestamp' data from firestore and display that value using 'yyyy-MM-dd hh:mm' format. How can I do it ?

1 Answers

Firestore will give you back a Timestamp object as I think you've figured out. It has a method .toDate() which will return a dart DateTime object.

Once you have the Date object, then you should use the DateFormat class as someone so helpfully pointed out in a comment without even linking to the documentation.

That would look something like this:

DateTime date = timestamp.toDate();
DateFormat(yyyy-MM-dd hh:mm").format(date);

although I haven't tested out the format function and dartpad doesn't seem to support the intl package, so you may need to play around with the format codes a bit.

Related