C# 日期时间格式的字符串

C# 日期时间格式的字符串,c#,sql-server,C#,Sql Server,我正在尝试将2010年10月10日上午12:00:00转换为dateTime。我正在用这个 DateTime.ParseExact(item.Birthey, "dd/MM/yyyy hh:mm:ss tt", null); 但我得到的字符串未被识别为有效的日期时间。 正确的方法是什么?只需简单地尝试Parse或ParseExact操作字符串命令,如: string stringDate= "10/10/2010 12:00:00 P.M."; var myDate = DateTime.Pa

我正在尝试将
2010年10月10日上午12:00:00
转换为dateTime。我正在用这个

DateTime.ParseExact(item.Birthey, "dd/MM/yyyy hh:mm:ss tt", null);
但我得到的
字符串未被识别为有效的日期时间。


正确的方法是什么?

只需简单地尝试
Parse
ParseExact
操作字符串命令,如:

string stringDate= "10/10/2010 12:00:00 P.M.";
var myDate = DateTime.Parse(stringDate.Replace(".", ""));
如果获得的原始数据以“a.m.”结尾,您也可以尝试以下代码示例。只需替换以下字符:

string stringDate= "10/10/2010 12:00:00 a.m.";
DateTime result = DateTime.ParseExact(stringDate.Replace(".", ""), 
                                 "dd/MM/yyyy hh:mm:ss tt", null);
试试看

  DateTime.ParseExact("10/10/2010 12:00:00 am", "dd/MM/yyyy hh:mm:ss tt", null).Dump();
上午
a.m.
中的
正在产生问题。您可以使用“替换”来删除
(与日期格式一样
只能在
上午
下午
中出现)

  DateTime.ParseExact("10/10/2010 12:00:00 a.m.".Replace(".",""), "dd/MM/yyyy hh:mm:ss tt", null).Dump();

尝试指定
CultureInfo.InvariantCulture
而不是
null
。在您的示例中,它使用
CultureInfo.CurrentCulture

在字符串中使用replace方法修剪
字符,它将起作用。
replace(.“,”),
您好,谢谢您的回复。我用过,但我的新datetime没有时间,只有日期。“2010年10月10日00:00:00”