Reputation: 19288
AS stated in the title: Is there an easy way to get all the days of the week as string(within a list ofcourse) in the users locale?
Upvotes: 6
Views: 9252
Reputation: 156
Using the intl package, the easiest way I found would be:
import 'package:intl/intl.dart';
var days = DateFormat.EEEE(Platform.localeName).dateSymbols.STANDALONEWEEKDAYS;
print(days) // => ["Sunday", "Monday", ..., "Saturday"]
You can replace STANDALONEWEEKDAYS
with WEEKDAYS
to get the names as they would appear within a sentence (e.g. first letter lowercase in some languages).
Also, you may use SHORTWEEKDAYS
and STANDALONESHORTWEEKDAYS
respectively to get the weekday abbreviations. For even shorter abbreviations, use NARROWWEEKDAYS
or STANDALONENARROWWEEKDAYS
.
Upvotes: 14
Reputation: 41
In case you need the current day or month localized with your phone settings
import 'dart:io';
import 'package:intl/date_symbol_data_local.dart';
final String defaultLocale = Platform.localeName; // Phone local
// String defaultLocale = "pt_BR"; // "en_US" etc. you can define yours as well
// add to init
@override
void initState() {
super.initState();
initializeDateFormatting(defaultLocale);
}
static String _getLocalizedWeekDay(String local, DateTime date) {
final formatter = DateFormat(DateFormat.WEEKDAY, local);
return formatter.format(date);
}
static String _getLocalizedMonth(String local, DateTime date) {
final formatter = DateFormat(DateFormat.MONTH, local);
return formatter.format(date);
}
Upvotes: 0
Reputation: 7100
My suggestion is:
static List<String> getDaysOfWeek([String locale]) {
final now = DateTime.now();
final firstDayOfWeek = now.subtract(Duration(days: now.weekday - 1));
return List.generate(7, (index) => index)
.map((value) => DateFormat(DateFormat.WEEKDAY, locale)
.format(firstDayOfWeek.add(Duration(days: value))))
.toList();
}
The idea is we define the date of the first day in the current week depending on current week day. Then just do loop 7 times starting from calculated date, add 1 day on each iteration and collect the result of DateFormat().format method with pattern DateFormat.WEEKDAY. To increase performance you can use lazy initialization. For example:
/// Returns a list of week days
static List<String> _daysOfWeek;
static List<String> get daysOfWeek {
if (_daysOfWeek == null) {
_daysOfWeek = getDaysOfWeek(); // Here you can specify your locale
}
return _daysOfWeek;
}
Upvotes: 12
Reputation: 30879
After doing:
import 'package:intl/date_symbol_data_local.dart';
String localeName = "pt_BR"; // "en_US" etc.
initializeDateFormatting(localeName);
Use this:
static List<String> weekDays(String localeName) {
DateFormat formatter = DateFormat(DateFormat.WEEKDAY, localeName);
return [DateTime(2000, 1, 3, 1), DateTime(2000, 1, 4, 1), DateTime(2000, 1, 5, 1),
DateTime(2000, 1, 6, 1), DateTime(2000, 1, 7, 1), DateTime(2000, 1, 8, 1),
DateTime(2000, 1, 9, 1)].map((day) => formatter.format(day)).toList();
}
I'm not sure it qualifies as "easy". Maybe someone here can come up with a better answer.
Upvotes: 3