dateStringFormatter function
Convert a DateTime date
to String (in French)
• You can specify the dateSeparator
, by default = "/"...
• You can specify the hourSeparator
, by default = ":"...
• You can tell if the String needs to contain dateOnly
or hourOnly
, by default = "false"...
• You can tell if the String needs to be longVersion
or monthVersion
or dayVersion
, by default = "false"...
Implementation
String dateStringFormatter(DateTime? date,
{String dateSeparator = "/",
String hourSeparator = ":",
bool dateOnly = false,
bool hourOnly = false,
bool longVersion = false,
bool monthVersion = false,
bool dayVersion = false}) {
// print(date);
String _toReturn = "";
final _receivedDate = date;
if (_receivedDate != null) {
String _theDay = _receivedDate.day.toString().forceInitialZeros;
String _theMonth = _receivedDate.month.toString().forceInitialZeros;
String _theYear = _receivedDate.year.toString();
String _theHour = _receivedDate.hour.toString().forceInitialZeros;
String _theMinute = _receivedDate.minute.toString().forceInitialZeros;
String _localDateSeparator = dateSeparator;
if (monthVersion) {
longVersion = true;
}
if (longVersion || dayVersion) {
_theDay = _receivedDate.day.toString();
if (_receivedDate.day == 1) {
_theDay = _theDay + "er";
}
_theMonth = moisLongs[_receivedDate.month - 1];
_localDateSeparator = " ";
}
if (monthVersion) {
_toReturn = "$_theMonth$_localDateSeparator$_theYear";
} else if (dayVersion) {
_toReturn = joursLongs[_receivedDate.weekday - 1] + " $_theDay";
} else if (dateOnly) {
_toReturn =
"$_theDay$_localDateSeparator$_theMonth$_localDateSeparator$_theYear";
} else if (hourOnly) {
_toReturn = "$_theHour$hourSeparator$_theMinute";
} else if ((_theHour == "00") && (_theMinute == "00")) {
_toReturn =
"$_theDay$_localDateSeparator$_theMonth$_localDateSeparator$_theYear";
} else {
_toReturn =
"$_theDay$_localDateSeparator$_theMonth$_localDateSeparator$_theYear @ $_theHour$hourSeparator$_theMinute";
}
} else {
_toReturn = "—";
}
// print(_toReturn);
return _toReturn;
}