How do I make Flutter firebase Timestamp to show only date

Please help me with this. After getting the timestamp from firebase, what it is displaying is this format 2019-04-16 12:18:06.018950 I am getting this as a stream from my firebase but I want it to be in this format 2019-04-16 just the date and not time.

Upvotes: 1

Views: 2366

Answers (1)

JideGuru
JideGuru

Reputation: 7660

  1. You can achieve this using the intl package
String timeString = '2019-04-16 12:18:06.018950';
DateTime date = DateTime.parse(timeString);
print(DateFormat('yyyy-MM-dd').format(date)); // prints 2019-04-16

make sure to add the intl package to the dependencies in your pubspec

intl: ^0.16.1

and also import it into your dart file

 import 'package:intl/intl_browser.dart';

OR

  1. you can use the split method on the time string
String timeString = '2019-04-16 12:18:06.018950';
print(timeString.split(" ")[0]); // prints 2019-04-16

Upvotes: 3

Related Questions