I've got a Datepicker and I'd like to allow multiple formats for users. For example, for the date January 5th 2014
I want to allow the following formats:
- 05-01-2014 :
dd-MM-yyyy
- 5-01-2014 :
d-MM-yyyy
- 5-1-2014 :
d-M-yyyy
- 5-01-14 :
d-MM-yy
- 5-1-14 :
d-M-yy
- 05-1-2014 :
dd-M-yyyy
- 05-1-14 :
dd-M-yy
- 05-01-14 :
dd-MM-yy
- 05012014 :
ddMMyyyy
- 050114 :
ddMMyy
512014 :dMyyyy
5114 :dMyy
I did create the following test-converter:
public class DatepickerConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null) return null;
return ((DateTime)value).ToString("dd-MM-yyyy", CultureInfo.InvariantCulture);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (string.IsNullOrWhiteSpace(value as string)) return null;
DateTime dt = DateTime.MinValue;
string[] checks = {
"dd-MM-yyyy",
"d-MM-yyyy",
"d-MM-yy",
"d-M-yyyy",
"d-M-yy",
"dd-M-yyyy",
"dd-M-yy",
"d-M-yyyy",
"d-M-yy",
"ddMMyyyy",
"ddMMyy",
//"dMyyyy",
//"dMyy",
};
for(int i = 0; i < checks.Length; i++)
{
try
{
dt = DateTime.ParseExact(value as string, checks[i], CultureInfo.InvariantCulture);
if (dt != null) break;
}
catch (Exception){ }
}
return dt as DateTime?;
}
}
I have however one problem, and one concern.
The problem is that I've never assigned a converter to a DatePicker. I've googled it and found this stackoverflow answer, but it doesn't seem to go to the converter in debug mode.. FIXED
And my concern is that using this for-loop every time the user inputs something is a bit performance-counterproductive. I know I can add a Property-Delay so I can do this formatting when the User has put in the entire date, instead of doing this per character the user puts in, but still, isn't there a more efficient / more readable solution than checking all formats one by one?