Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: Display local datetimes correctly

let's say I have a DateTime object and I want to display it in the correct local format.

If I do the following on a German device I get this:

dateTime.toLocal().toString()
// Prints
2022-05-28 23:29:19.518

However, I would expect or desire more something like this for a German device: 28.5.2022 23:29:19

I know that I can format the DateTime but that would just be hardcoding it for a certain locale.

Weirdly enough all the solutions that I found for this on StackOverflow are either hardcoding the format or only apply to Dart, not Flutter.

What is the correct way to display a local datetime in Flutter?

like image 331
Josip Domazet Avatar asked Nov 18 '25 13:11

Josip Domazet


1 Answers

You can use this package intl and localise dates like

var format = DateFormat.yMd('ar');
var dateString = format.format(DateTime.now());
like image 159
Kaushik Chandru Avatar answered Nov 21 '25 05:11

Kaushik Chandru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!