我正在构建一个ViewModel
,其中我正在尝试格式化DateTime
MMMM dd, yyyy
格式,但它正在抛出错误
No overload for method 'ToString' takes 1 argument
我使用http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx来提供代码
DateUpdated.ToString("MMMM dd, yyyy")
但这显然是错误的。格式化日期的正确方法是什么?
视图模型:
public class ConversionFactorsVM
{
[Required]
public int TankID { get; set; }
[ReadOnly(true), DisplayName("Product Name")]
public string ProductName { get; set; }
[ReadOnly(true), DisplayName("Product ID")]
public int Productnumber { get; set; }
[Required, Range(0, 200.9, ErrorMessage = "Gravity must be between 0 and 200.9")]
public decimal Gravity { get; set; }
[Required, Range(0, 200.9, ErrorMessage = "Temperature must be between 0 and 200.9")]
public decimal Temperature { get; set; }
[ReadOnly(true)]
public decimal Factor { get; set; }
public DateTime? DateUpdated { get; set; }
[DisplayName("Last Updated")]
public string LastUpdate
{
get
{
if (DateUpdated.HasValue)
{
return DateUpdated.ToString("MMMM dd, yyyy");
}
else
{
return "Never Updated.";
}
}
}
}
答案 0 :(得分:11)
您必须使用Nullable<DateTime>.Value
,因为那是DateTime
:
return DateUpdated.Value.ToString("MMMM dd, yyyy");
答案 1 :(得分:0)