I have a similar problem using the solution above. For me, the string "this [" Time "]. ToString ()" is like "2015/6/12 15:31:24", and the format parameter of the DateTime.ParseExact method is "yyyy-MM-dd HH: mm: ss" . Thus, this method cannot parse a string with a given format. Alternatively you can do, for example, below
if(this["Time"] is string)
{
return DateTime.ParseExact(
this["Time"].ToString(),
"yyyy-MM-dd HH:mm:ss",
CultureInfo.InvariantCulture);
}
else
{
DateTime time = (DateTime)this["Time"];
return DateTime.ParseExact(
time.ToString("yyyy-MM-dd HH:mm:ss"),
"yyyy-MM-dd HH:mm:ss",
CultureInfo.InvariantCulture);
}
source
share