How to convert Timestamp to DateTime from firebase with flutter

Issue

I have Timestamp item in firebase. I get the item from dart code timestamp type.
It shows like ‘Timestamp(seconds=1590903768, nanoseconds=26999000)’ as it is.

I would like to show on my Application only Date like ‘2020-06-01′ or ’06-01’.

Please give me advice.

Solution

Timestamp class has a toDate function that converts it to a DateTime object. See this for more information. Any formatting you want to do in converting it to a string can be done more easily now with intl package formatters.

Example:

Timestamp stamp = Timestamp.now();
DateTime date = stamp.toDate();

Answered By – Christopher Moore

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published