我正在尝试创建一个将 1225 转换为 12/25/13 的文本框。在做了很多研究之后,我认为“DateTime.TryParseExact”是我需要使用的,但我无法让它工作。这是我的代码:
CultureInfo provider = CultureInfo.InvariantCulture;
DateTime dateValue;
string[] DateTimeFormats = new string[]{
"MM/dd/yy","MM/dd/yy HH:mm","MM/dd/yy HH:mm:ss","HH:mm","HH:mm:ss",
"M/d/yy","M/d/yy HH:mm","M/d/yy HH:mm:ss",
"MM/dd/yyyy","MM/dd/yyyy HH:mm","MM/dd/yyyy HH:mm:ss",
"MMddyy","MMddyyHHmm","MMddyyHHmmss","HHmm","HHmmss",
"MMddyyyy","MMddyyyyHHmm","MMddyyyyHHmmss",
"MMddyy HHmm","MMddyy HHmmss",
"MMddyyyy HHmm","MMddyyyy HHmmss",
"yyyyMMdd","yyyyMMddHHmm","yyyyMMddHHmmss"};
if (DateTime.TryParseExact(TheTextBox.Text, DateTimeFormats, provider, DateTimeStyles.None, out dateValue))
{
TheTextBox.Text = dateValue.ToString("d MMMM yyyy");
}
任何想法如何解决这一问题?