formatDateString method
Converts a date string into a desired target format.
originFormat: Optional. If provided, parses the string using this format. If null, attempts to parse using common formats viatoDateTime.targetFormat: The desired output format. Defaults to 'dd/MM/yyyy'.
Returns an empty string if parsing or formatting fails.
Implementation
String formatDateString({
String? originFormat,
String targetFormat = 'dd/MM/yyyy',
}) {
if (this == null || this!.isEmpty) return '';
// Parse the string into DateTime
final dt = toDateTime(originFormat: originFormat);
if (dt == null) return '';
// Format DateTime into target format using supported locales
for (final locale in _supportedLocales) {
try {
return DateFormat(targetFormat, locale).format(dt);
} catch (e) {
debugPrint('formatDateString failed for locale $locale: $e');
}
}
return '';
}