raghi
raghi

Reputation: 25

how to show timestamp from firestore in dd/mm/yy format in flutter

how to show datetime(timestamp format) form firebase firestore in (dd/mm/yy, hh:mm:ss) in flutter. please see the images attachedfirebase firestore data and my code is my code in vscode

Upvotes: 0

Views: 837

Answers (2)

Aayush Shah
Aayush Shah

Reputation: 703

You can simply call toDate() function to the dateTime or your firebase timestamp. You can also convert them into desired format by using DateFormat class Here is a small function which will return time like 12:37 AM :

import 'package:intl/intl.dart';   //add this import statement for using DateTime class

String getTime(var time) {
    final DateFormat formatter = DateFormat('dd/MM/yyyy, hh:mm:ss aa');  //your date format here
    var date = time.toDate();
    return formatter.format(date);
  }

This function will convert your timestamp object to provided format eg.: July 23, 2021 at 9:22:29 PM UTC+5:30 -> 23/07/2021, 9:22:29 PM You can refer this document for detailed date formatting.

Upvotes: 1

Ryan
Ryan

Reputation: 131

You can first parse the date to get a DateTime object by using DateTime.parse(string_from_firebase).

Then use the DateFormat class from the intl package.

final DateTime dateToBeFormatted = DateTime.parse(string);
final df = DateFormat('dd/MM/yyyy');
final formatted = df.format(dateToBeFormatted);

Upvotes: 0

Related Questions