dateFormatter function
Convert a String dateString
to DateTime
• You can specify the dateSeparator
, by default = "/"...
• You can specify the hourSeparator
, by default = ":"...
• You can tell if the String contains only the date by putting "true" to noHour
, by default = "false"...
• You can tell the String pattern in format
, by default = "DMY"...
Implementation
DateTime dateFormatter(String dateString,
{String dateSeparator = "/",
String hourSeparator = ":",
bool noHour = false,
String format = "DMY"}) {
// print("dateString = " + dateString);
DateTime _toReturn = DateTime.now();
int dayIndex = 0;
int monthIndex = 1;
int yearIndex = 2;
for (var $i = 0; $i < format.length; $i++) {
if (format[$i] == "D") {
dayIndex = $i;
} else if (format[$i] == "M") {
monthIndex = $i;
} else if (format[$i] == "Y") {
yearIndex = $i;
}
}
String _theDate = dateString.split(" ")[0];
int _theDay = int.parse(_theDate.split(dateSeparator)[dayIndex]);
int _theMonth = int.parse(_theDate.split(dateSeparator)[monthIndex]);
int _theYear = int.parse(_theDate.split(dateSeparator)[yearIndex]);
/*print("&&_" +
_theDay.toString() +
"_&&_" +
_theMonth.toString() +
"_&&_" +
_theYear.toString() +
"_&&");*/
int _theHour = 0;
int _theMinute = 0;
if ((dateString.split(" ").length > 1) && (noHour == false)) {
String _theTime = dateString.split(" ")[1];
if (_theTime == "@") {
_theTime = dateString.split(" ")[2];
// print("_theTime => CHANGED <= ");
}
// print("_theTime => " + _theTime + " <= " + hourSeparator);
if (_theTime != "@") {
_theHour = int.parse(_theTime.split(hourSeparator)[0]);
_theMinute = int.parse(_theTime.split(hourSeparator)[1]);
}
}
// print("##_" + _theHour.toString() + "_##_" + _theMinute.toString() + "_##");
_toReturn = DateTime(_theYear, _theMonth, _theDay, _theHour, _theMinute);
// print("%%_" + _toReturn.toString() + "_%%");
return _toReturn;
}