formattedDate static method
String
formattedDate(
- DateTime dateTime, {
- CustomDatetimeFormatType formatType = CustomDatetimeFormatType.dateTime,
- String? locale,
- String? dividerDateHour = '-',
- String outputTimeFormat = intl.DateFormat.HOUR_MINUTE,
Implementation
static String formattedDate(
DateTime dateTime, {
CustomDatetimeFormatType formatType = CustomDatetimeFormatType.dateTime,
String? locale,
String? dividerDateHour = '-',
String outputTimeFormat = intl.DateFormat.HOUR_MINUTE,
}) {
var localeString = locale;
if (kIsWeb) {
localeString ??= html.window.navigator.language;
}
DateTime localDateTime = dateTime.toLocal();
String dateString = intl.DateFormat.yMd(
locale ?? localeString,
).format(localDateTime);
String timeString = intl.DateFormat(
outputTimeFormat,
locale ?? localeString,
).format(localDateTime);
// replace to avoid an issue during the tests
timeString = timeString.replaceAll(String.fromCharCode(0x202F), ' ');
String string = '';
switch (formatType) {
case CustomDatetimeFormatType.date:
string = dateString;
break;
case CustomDatetimeFormatType.time:
string = timeString;
break;
case CustomDatetimeFormatType.dateTime:
string = '''$dateString ${dividerDateHour != null ? '$dividerDateHour ' : ''}'''
'''$timeString''';
break;
}
return string;
}