Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format DateTime in Flutter

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.

Current approach

DateTime now = DateTime.now();
currentTime = new DateTime(now.year, now.month, now.day, now.hour, now.minute);
 Text('$currentTime'), 

Result

YYYY-MM-JJ HH-MM:00.000

Question

How can I remove the :00.000 part?

like image 633
Nitneuq Avatar asked Sep 28 '22 08:09

Nitneuq


People also ask

How do I change the format of a DateTime Flutter?

To format DateTime in Flutter using a standard format, you need to use the intl library and then use the named constructors from the DateFormat class. Simply write the named constructor and then call the format() method with providing the DateTime.


1 Answers

You can use DateFormat from intl package.

import 'package:intl/intl.dart';

DateTime now = DateTime.now();
String formattedDate = DateFormat('yyyy-MM-dd – kk:mm').format(now);
like image 441
boformer Avatar answered Oct 09 '22 10:10

boformer