neue Datumsformate (DE + EN) hinzugefügt inkl. kurze Monatsnamen

This commit is contained in:
Daniel Schick 2022-12-09 09:07:15 +01:00
parent 177835440c
commit bd93f98460

View File

@ -420,7 +420,7 @@ namespace ENI2.Excel
{ {
var val = _nameDict[lookup].RefersToRange.Value; var val = _nameDict[lookup].RefersToRange.Value;
if (val is DateTime) if (val is DateTime)
{ {
date = val; date = val;
} }
else if (val is double) else if (val is double)
@ -446,22 +446,37 @@ namespace ENI2.Excel
date = tmpDate; date = tmpDate;
} }
if (date == null)
{
string[] formats = { "d/M/yyyy", "dd/M/yyyy", "d/MM/yyyy", "dd/MM/yyyy", "d/MMM/yyyy", "dd/MMM/yyyy" };
if (DateTime.TryParseExact(val, formats, NumberFormatInfo.InvariantInfo, DateTimeStyles.None, out DateTime tmpDate))
date = tmpDate;
}
if (date == null)
{
CultureInfo en = CultureInfo.CreateSpecificCulture("en-US");
string[] formats = { "d/M/yyyy", "dd/M/yyyy", "d/MM/yyyy", "dd/MM/yyyy", "d/MMM/yyyy", "dd/MMM/yyyy" };
if (DateTime.TryParseExact(val, formats, en, DateTimeStyles.None, out DateTime tmpDate))
return tmpDate;
}
if (date != null) if (date != null)
{ {
if ((date.Value < new DateTime(1899, 1, 1)) || (date.Value > new DateTime(2130, 1, 1))) if ((date.Value < new DateTime(1899, 1, 1)) || (date.Value > new DateTime(2130, 1, 1)))
{ {
date = null; // this can't be right date = null; // this can't be right
} }
} }
} }
return date; return date;
} }
catch (Exception) catch (Exception)
{ {
_log.WarnFormat("error parsing datetime for lookup {0}", lookup); _log.WarnFormat("error parsing datetime for lookup {0}", lookup);
return null; return null;
} }
} }
internal DateTime? ReadDateTime(string dateField, string timeField) internal DateTime? ReadDateTime(string dateField, string timeField)
@ -690,19 +705,16 @@ namespace ENI2.Excel
string dateString = ReadCellAsText(sheetName, range); string dateString = ReadCellAsText(sheetName, range);
if (dateString != null) if (dateString != null)
{ {
CultureInfo provider = CultureInfo.InvariantCulture; string[] formats = { "yyyy/MM/dd HH:mm", "yyyy/MM/dd", "dd.MM.yyyy", "d/M/yyyy", "dd/M/yyyy", "d/MM/yyyy", "dd/MM/yyyy", "d/MMM/yyyy", "dd/MMM/yyyy" };
const string dateTimeFormat = "yyyy/MM/dd HH:mm"; if (DateTime.TryParseExact(dateString, formats, NumberFormatInfo.InvariantInfo, DateTimeStyles.None, out DateTime tmpDate))
if (DateTime.TryParseExact(dateString, dateTimeFormat, provider, DateTimeStyles.None, out DateTime tmpDate)) return tmpDate;
return tmpDate.ToUniversalTime(); CultureInfo en = CultureInfo.CreateSpecificCulture("en-US");
const string dateFormat = "yyyy/MM/dd"; if (DateTime.TryParseExact(dateString, formats, en, DateTimeStyles.None, out DateTime tmpDate2))
if (DateTime.TryParseExact(dateString, dateFormat, provider, DateTimeStyles.None, out DateTime tmpDate2))
return tmpDate2; return tmpDate2;
const string dateFormat2 = "dd.MM.yyyy";
if (DateTime.TryParseExact(dateString, dateFormat2, provider, DateTimeStyles.None, out DateTime tmpDate3))
return tmpDate3;
return null; return null;
} }
else return null; else
return null;
} }