I am trying to display the current DateTime
in a Text
widget after tapping on a button. The following works, but I\'d like to change the format.
Add intl
package to your pubspec.yaml
file.
import 'package:intl/intl.dart';
DateFormat dateFormat = DateFormat("yyyy-MM-dd HH:mm:ss");
Converting DateTime object to String
String string = dateFormat.format(DateTime.now());
Converting String to DateTime object
DateTime dateTime = dateFormat.parse("2019-07-19 8:40:23");
Here's my simple solution. That does not require any dependency.
However, the date will be in string format. If you want the time then change the substring values
print(new DateTime.now()
.toString()
.substring(0,10)
); // 2020-06-10
there is some change since the 0.16 so here how i did,
import in the pubspec.yaml
dependencies:
flutter:
sdk: flutter
intl: ^0.16.1
then use
txdate= DateTime.now()
DateFormat.yMMMd().format(txdate)