Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Locale Date String to DateTime

I want to convert date String* to DateTime object.

  • String contains month name in Turkish language like below

My String (from API) - ”10 Mart 2021 16:38”

My Locale - Turkey [‘tr’]

How can I convert?

Thanks you!

like image 593
Ferhat Ismayilov Avatar asked Jun 12 '26 23:06

Ferhat Ismayilov


1 Answers

Try the following. Only the en_US locale does not require any initialization. Other locales have to be initialized. For more info visit https://pub.dev/documentation/intl/latest/intl/DateFormat-class.html

import 'package:intl/intl.dart';
import 'package:intl/date_symbol_data_local.dart';

// Then somewhere in your code:
initializeDateFormatting('tr_TR', null).then((_) {
      final dateAsString = '10 Mart 2021 16:38';
      final format = new DateFormat('dd MMMM yyyy HH:mm', 'tr_TR');
      final date = format.parse(dateAsString);
});
like image 96
hnnngwdlch Avatar answered Jun 15 '26 22:06

hnnngwdlch



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!