将字符串转换为任何格式的日期时间
Convert string to datetime in any format
目标 :
想要将任何格式的日期时间转换为去德文化
示例日期时间 :
2016 年 3 月 30 日 2:38:20 下午
2016 年 4 月 4 日 11:08:10 上午
我试过的
protected void Page_Load(object sender, EventArgs e)
{
string dt1Valid = CheckDateFormat("3/30/2016 2:38:20 PM");
}
public string CheckDateFormat(string checkDate)
{
string formats1 = getFormat(checkDate);
DateTime parsedDateTime;
DateTimeFormatInfo ukDtfi = new CultureInfo("de-DE", false).DateTimeFormat;
if (!DateTime.TryParseExact(checkDate, formats1, new CultureInfo("de-DE"),
DateTimeStyles.None, out parsedDateTime))
{
return Convert.ToDateTime(parsedDateTime.ToString()).ToString(ukDtfi.ShortDatePattern + " " + ukDtfi.LongTimePattern);
}
else
return "";
}
public string getFormat(string checkDate)
{
string[] formats = {"M/d/yyyy", "MM/dd/yyyy",
"d/M/yyyy", "dd/MM/yyyy",
"yyyy/M/d", "yyyy/MM/dd",
"M-d-yyyy", "MM-dd-yyyy",
"d-M-yyyy", "dd-MM-yyyy",
"yyyy-M-d", "yyyy-MM-dd",
"M.d.yyyy", "MM.dd.yyyy",
"d.M.yyyy", "dd.MM.yyyy",
"yyyy.M.d", "yyyy.MM.dd",
"M,d,yyyy", "MM,dd,yyyy",
"d,M,yyyy", "dd,MM,yyyy",
"yyyy,M,d", "yyyy,MM,dd",
"M d yyyy", "MM dd yyyy",
"d M yyyy", "dd MM yyyy",
"yyyy M d", "yyyy MM dd",
"M/d/yyyy hh:mm:ss tt", "MM/dd/yyyy hh:mm:ss tt",
"d/M/yyyy hh:mm:ss tt", "dd/MM/yyyy hh:mm:ss tt",
"yyyy/M/d hh:mm:ss tt", "yyyy/MM/dd hh:mm:ss tt",
"M-d-yyyy hh:mm:ss tt", "MM-dd-yyyy hh:mm:ss tt",
"d-M-yyyy hh:mm:ss tt", "dd-MM-yyyy hh:mm:ss tt",
"yyyy-M-d hh:mm:ss tt", "yyyy-MM-dd hh:mm:ss tt",
"M.d.yyyy hh:mm:ss tt", "MM.dd.yyyy hh:mm:ss tt",
"d.M.yyyy hh:mm:ss tt", "dd.MM.yyyy hh:mm:ss tt",
"yyyy.M.d hh:mm:ss tt", "yyyy.MM.dd hh:mm:ss tt",
"M,d,yyyy hh:mm:ss tt", "MM,dd,yyyy hh:mm:ss tt",
"d,M,yyyy hh:mm:ss tt", "dd,MM,yyyy hh:mm:ss tt",
"yyyy,M,d hh:mm:ss tt", "yyyy,MM,dd hh:mm:ss tt",
"M d yyyy hh:mm:ss tt", "MM dd yyyy hh:mm:ss tt",
"d M yyyy hh:mm:ss tt", "dd MM yyyy hh:mm:ss tt",
"yyyy M d hh:mm:ss tt", "yyyy MM dd hh:mm:ss tt"
};
DateTime dateValue;
foreach (string dateStringFormat in formats)
{
if (DateTime.TryParseExact(checkDate, dateStringFormat,
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out dateValue))
//Console.WriteLine("Converted '{0}' to {1}.", dateStringFormat, dateValue.ToString("yyyy-MM-dd"));
return dateStringFormat;
}
return null;
}
我期望的结果
30.03.2016 14:38:20
04.04.2016 11:08:10
我得到的结果
01.01.0001 00:00:00
01.01.0001 00:00:00
回答起来很简单:这是不可能的,所以不要再尝试了。
证明:任何格式包括M.d.yyyy
和d.M.yyyy
。在这种情况下,1.2.2016 可以表示
- 二月一日或
- 一月二号。
结论:不可能从任何格式进行翻译,因为没有附加信息就不可能解析不明确的日期。
注意:这与 C# 或您选择的技术无关。您的 要求 已损坏。修复它们。
目标 :
想要将任何格式的日期时间转换为去德文化
示例日期时间 :
2016 年 3 月 30 日 2:38:20 下午
2016 年 4 月 4 日 11:08:10 上午
我试过的
protected void Page_Load(object sender, EventArgs e)
{
string dt1Valid = CheckDateFormat("3/30/2016 2:38:20 PM");
}
public string CheckDateFormat(string checkDate)
{
string formats1 = getFormat(checkDate);
DateTime parsedDateTime;
DateTimeFormatInfo ukDtfi = new CultureInfo("de-DE", false).DateTimeFormat;
if (!DateTime.TryParseExact(checkDate, formats1, new CultureInfo("de-DE"),
DateTimeStyles.None, out parsedDateTime))
{
return Convert.ToDateTime(parsedDateTime.ToString()).ToString(ukDtfi.ShortDatePattern + " " + ukDtfi.LongTimePattern);
}
else
return "";
}
public string getFormat(string checkDate)
{
string[] formats = {"M/d/yyyy", "MM/dd/yyyy",
"d/M/yyyy", "dd/MM/yyyy",
"yyyy/M/d", "yyyy/MM/dd",
"M-d-yyyy", "MM-dd-yyyy",
"d-M-yyyy", "dd-MM-yyyy",
"yyyy-M-d", "yyyy-MM-dd",
"M.d.yyyy", "MM.dd.yyyy",
"d.M.yyyy", "dd.MM.yyyy",
"yyyy.M.d", "yyyy.MM.dd",
"M,d,yyyy", "MM,dd,yyyy",
"d,M,yyyy", "dd,MM,yyyy",
"yyyy,M,d", "yyyy,MM,dd",
"M d yyyy", "MM dd yyyy",
"d M yyyy", "dd MM yyyy",
"yyyy M d", "yyyy MM dd",
"M/d/yyyy hh:mm:ss tt", "MM/dd/yyyy hh:mm:ss tt",
"d/M/yyyy hh:mm:ss tt", "dd/MM/yyyy hh:mm:ss tt",
"yyyy/M/d hh:mm:ss tt", "yyyy/MM/dd hh:mm:ss tt",
"M-d-yyyy hh:mm:ss tt", "MM-dd-yyyy hh:mm:ss tt",
"d-M-yyyy hh:mm:ss tt", "dd-MM-yyyy hh:mm:ss tt",
"yyyy-M-d hh:mm:ss tt", "yyyy-MM-dd hh:mm:ss tt",
"M.d.yyyy hh:mm:ss tt", "MM.dd.yyyy hh:mm:ss tt",
"d.M.yyyy hh:mm:ss tt", "dd.MM.yyyy hh:mm:ss tt",
"yyyy.M.d hh:mm:ss tt", "yyyy.MM.dd hh:mm:ss tt",
"M,d,yyyy hh:mm:ss tt", "MM,dd,yyyy hh:mm:ss tt",
"d,M,yyyy hh:mm:ss tt", "dd,MM,yyyy hh:mm:ss tt",
"yyyy,M,d hh:mm:ss tt", "yyyy,MM,dd hh:mm:ss tt",
"M d yyyy hh:mm:ss tt", "MM dd yyyy hh:mm:ss tt",
"d M yyyy hh:mm:ss tt", "dd MM yyyy hh:mm:ss tt",
"yyyy M d hh:mm:ss tt", "yyyy MM dd hh:mm:ss tt"
};
DateTime dateValue;
foreach (string dateStringFormat in formats)
{
if (DateTime.TryParseExact(checkDate, dateStringFormat,
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out dateValue))
//Console.WriteLine("Converted '{0}' to {1}.", dateStringFormat, dateValue.ToString("yyyy-MM-dd"));
return dateStringFormat;
}
return null;
}
我期望的结果
30.03.2016 14:38:20
04.04.2016 11:08:10
我得到的结果
01.01.0001 00:00:00
01.01.0001 00:00:00
回答起来很简单:这是不可能的,所以不要再尝试了。
证明:任何格式包括M.d.yyyy
和d.M.yyyy
。在这种情况下,1.2.2016 可以表示
- 二月一日或
- 一月二号。
结论:不可能从任何格式进行翻译,因为没有附加信息就不可能解析不明确的日期。
注意:这与 C# 或您选择的技术无关。您的 要求 已损坏。修复它们。