Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing date formatter's locale

Tags:

flutter

dart

am using Date formatter in my app to display some dates.. but i want this date to be shown in Arabic .. so i tried to change locale of the formatter like this this:

var now = new DateTime.now();
var formatter = DateFormat.yMMMd("ar_SA");
String formatted = formatter.format(now);
print(formatted);

but its not working ..

if i changed it to this:

var now = new DateTime.now();
var formatter = DateFormat.yMMMd("en_US");
String formatted = formatter.format(now);
print(formatted);

Will work fine .. but i want the date to be displayed in Arabic ..

how to achieve this? i want the date to be for example ١١ يناير ٢٠١٨.. how to do it?

like image 579
lamatat Avatar asked Nov 12 '18 14:11

lamatat


2 Answers

You need to use initializeDateFormatting to get the correct date formatter like this:

initializeDateFormatting("ar_SA", null).then((_) {
      var now = DateTime.now();
      var formatter = DateFormat.yMMMd('ar_SA');
      print(formatter.locale);
      String formatted = formatter.format(now);
      print(formatted);
    });

or with async-await you could do:

await initializeDateFormatting("ar_SA", null);
var now = DateTime.now();
var formatter = DateFormat.yMMMd('ar_SA');
print(formatter.locale);
String formatted = formatter.format(now);
print(formatted);

This results in the following output for me:

ar
١٢ نوفمبر ٢٠١٨
like image 171
Ringil Avatar answered Oct 15 '22 01:10

Ringil


and just make sure import data local

import 'package:intl/date_symbol_data_local.dart';
like image 26
PesaCoder Avatar answered Oct 14 '22 23:10

PesaCoder