您可以使用DateTime.Parse或DateTime.ParseExact
DateTime dateValue;
string dateString = "2/16/2008 12:15:12 PM";
try {
dateValue = DateTime.Parse(dateString);
Console.WriteLine("'{0}' converted to {1}.", dateString, dateValue);
}
catch (FormatException) {
Console.WriteLine("Unable to convert '{0}'.", dateString);
}
對於ParseExact
dateString = "Sun 15 Jun 2008 8:30 AM -06:00";
format = "ddd dd MMM yyyy h:mm tt zzz";
try
{
result = DateTime.ParseExact(dateString, format, provider);
Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
}
catch (FormatException)
{
Console.WriteLine("{0} is not in the correct format.", dateString);
}
你*確實*注意到* Relat編寫問題時出現的問題列表?那裏有很多重複的問題。 – 2011-03-15 15:51:04
發表之前,爲什麼不搜索發佈問題時發佈的上一篇文章 – Dotnet 2011-03-15 15:51:47
再次搜索,我發現這樣會更好一些(因爲它也指出'DateTime.TryParse'):[將字符串轉換爲C#中的DateTime ](http://stackoverflow.com/questions/1592653/convert-string-to-datetime-in-c/1592660#1592660) – 2011-03-15 15:55:09