'How to convert Timestamp to DateTime from firebase with flutter

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 1:[1]

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();

Solution 2:[2]

You can convert Timestamp DataType to DateTime then format the DateTime according to your requirements.

Example

add this following method in your dart file.

String formatTimestamp(Timestamp timestamp) {
  var format = new DateFormat('y-MM-d'); // <- use skeleton here
  return format.format(timestamp.toDate());
}

then get the TimeStamp from FireStore and pass it into this method

print(formatTimestamp(doc.data['timestamp']));

OutPut

year-month-day

Other available formats, use Skeleton to format.

 ICU Name                   Skeleton
 --------                   --------
 DAY                          d
 ABBR_WEEKDAY                 E
 WEEKDAY                      EEEE
 ABBR_STANDALONE_MONTH        LLL
 STANDALONE_MONTH             LLLL
 NUM_MONTH                    M
 NUM_MONTH_DAY                Md
 NUM_MONTH_WEEKDAY_DAY        MEd
 ABBR_MONTH                   MMM
 ABBR_MONTH_DAY               MMMd
 ABBR_MONTH_WEEKDAY_DAY       MMMEd
 MONTH                        MMMM
 MONTH_DAY                    MMMMd
 MONTH_WEEKDAY_DAY            MMMMEEEEd
 ABBR_QUARTER                 QQQ
 QUARTER                      QQQQ
 YEAR                         y
 YEAR_NUM_MONTH               yM
 YEAR_NUM_MONTH_DAY           yMd
 YEAR_NUM_MONTH_WEEKDAY_DAY   yMEd
 YEAR_ABBR_MONTH              yMMM
 YEAR_ABBR_MONTH_DAY          yMMMd
 YEAR_ABBR_MONTH_WEEKDAY_DAY  yMMMEd
 YEAR_MONTH                   yMMMM
 YEAR_MONTH_DAY               yMMMMd
 YEAR_MONTH_WEEKDAY_DAY       yMMMMEEEEd
 YEAR_ABBR_QUARTER            yQQQ
 YEAR_QUARTER                 yQQQQ
 HOUR24                       H
 HOUR24_MINUTE                Hm
 HOUR24_MINUTE_SECOND         Hms
 HOUR                         j
 HOUR_MINUTE                  jm
 HOUR_MINUTE_SECOND           jms
 MINUTE                       m
 MINUTE_SECOND                ms
 SECOND                       s

I hope this will be helpfull !!

Solution 3:[3]

You get back a map from firestore. Assuming you have named it "creationDate", you can convert the returned timestamp to a DateTime object using the toDate() dart function. The "simplest" way to format the datetime is to use the intl package

// Map From firestore
Map data = documentSnapshot.data();
var creationDate = data['creationDate'].toDate();

//Format using intl package
DateFormat _dateFormat = DateFormat('y-MM-d');
String formattedDate =  _dateFormat.format(dateTime);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Christopher Moore
Solution 2
Solution 3 Blacksmith