我收到此错误:如何字符串转换成datetime在C#
An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code
Additional information: String was not recognized as a valid DateTime.
我的两个类如下所示:
public class GigFormViewModel
{
public string Venue { get; set; }
public string Date { get; set; }
public string Time { get; set; }
public byte Genre { get; set; }
public IEnumerable<Genre> Genres { get; set; }
public DateTime DateTime
{
get { return DateTime.Parse(string.Format("{0} {1}", Date, Time)); }
}
}
和
public class Gig
{
public int Id { get; set; }
[Required]
public ApplicationUser Artist { get; set; }
[Required]
public string ArtistId { get; set; }
public DateTime DateTime { get; set; }
[Required]
[StringLength(255)]
public string Venue { get; set; }
public Genre Genre { get; set; }
[Required]
public byte GenreId { get; set; }
}
改为使用'ParseExact'或'TryParseExact'。 –
你为什么不告诉我们字符串? – TaW
您可以为“日期”和“时间”属性显示一个示例输入吗? –