Possible Duplicate:
ParseExact a string to DateTime fails
I need to parse
Oct 19 13:55
into a DateTime
object.
Which culture would work for it ?
Possible Duplicate:
ParseExact a string to DateTime fails
I need to parse
Oct 19 13:55
into a DateTime
object.
Which culture would work for it ?
您可以使用DateTime.ParseExact。
由于您的原始字符串没有year
信息,因此它将考虑 DateTime 对象的当前年份。
string temp = "Oct 19 13:55";
DateTime dt = DateTime.ParseExact(temp, "MMM dd HH:mm", CultureInfo.InvariantCulture);
在dt
你会得到:
dt = {19/10/2012 下午 1:55:00}
编辑:由于原始数据不包含具有单个数字日期和小时的日期,因此可以尝试以下格式的日期,例如:
Nov 8 1:44
DateTime dt = DateTime.ParseExact(temp, "MMM d H:mm", CultureInfo.InvariantCulture);
(这两种格式都适用)
DateTime dt = new DateTime();
string str="Oct 19 13:55";
CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
bool ok = DateTime.TryParseExact((str == null ? "" : str.ToString().Trim()), "MMM dd HH:mm", culture, DateTimeStyles.None, out dt);
console.write(dt);