5

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.

tajihiro
  • 2,293
  • 7
  • 39
  • 60
  • I'm not a dart expert, but does this help? https://stackoverflow.com/questions/16126579/how-do-i-format-a-date-with-dart – djthoms Jun 01 '20 at 04:21
  • This page should help: https://stackoverflow.com/questions/52996707/flutter-app-error-type-timestamp-is-not-a-subtype-of-type-datetime – jdaz Jun 01 '20 at 04:25
  • https://stackoverflow.com/questions/50632217/dart-flutter-converting-timestamp – Yudhishthir Singh Jun 01 '20 at 04:26

5 Answers5

14

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();
Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
4

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

Note: you will have to install the intl package in your flutter project to make this code work.

Example

add the following method in your dart file.

String formatTimestamp(Timestamp timestamp) {
  var format = new DateFormat('y-M-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

For 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 helpful!!

Kamal Panara
  • 482
  • 7
  • 16
  • The actual timestamp saved on Firestore is 13-Nov-21, but after using this code, the date shown is 13-10-21 (13-Oct-21). How to fix this..?? – Madhav Kumar Nov 19 '21 at 13:21
  • 1
    I got the answer, `DateFormat('d-MM-y')` will show November as 10, and Jan as 0, because the counting starts from 0. So, instead I used, `DateFormat('d-MMM-y')`, it shows 13-Nov-21, and not 13-10-21. – Madhav Kumar Nov 19 '21 at 18:56
1

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);
Blacksmith
  • 712
  • 8
  • 21
0

If you want Hours and minutes(hh:mm) from timestamp, Simply create following method which gives you string in return.

String convertTimestampToHm(Timestamp timestamp) {
var format = DateFormat('Hm'); // convert into hours and minutes
return format.format(timestamp.toDate());

}

NOTE: - You can always use Skeleton base on requiremrnt.

Pranav Dave
  • 393
  • 5
  • 7
0

There are different ways to convert TimeStamp to DateTime which differs based on the scenario. I have summarize all the possible ways, Pick the one which suits your case !!


Conversion of Firebase timestamp to DateTime:

  1. document['timeStamp'].toDate()
    
  2. (document["timeStamp"] as Timestamp).toDate()
    
  3. DateTime.fromMillisecondsSinceEpoch(document['timeStamp'].millisecondsSinceEpoch);
    
  4. Timestamp.fromMillisecondsSinceEpoch(document['timeStamp'].millisecondsSinceEpoch).toDate();
    
  5. If timeStamp is in microseconds use:

    DateTime.fromMicrosecondsSinceEpoch(timestamp * 1000000);
    
  6. If timeStamp is in milliseconds use:

    DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
    
  7. Add the following function in your dart file.

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

    call it as formatTimestamp(document['timestamp'])

krishnaacharyaa
  • 14,953
  • 4
  • 49
  • 88