Flutter Locale Date String to DateTime

Issue

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!

Solution

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);
});

Answered By – hnnngwdlch

Answer Checked By – Katrina (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.