I am building out a ViewModel
and in it I am trying to format a DateTime
in MMMM dd, yyyy
format but it is throwing the error
No overload for method 'ToString' takes 1 argument
I used http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx to come up with the code
DateUpdated.ToString("MMMM dd, yyyy")
but that is apparently wrong. What is the correct way to format the date?
ViewModel:
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.";
}
}
}
}
您必须使用Nullable<DateTime>.Value
因为那是DateTime
:
return DateUpdated.Value.ToString("MMMM dd, yyyy");
You should use Value
propery for getting the value of Nullable<DateTime>
Gets the value of the current
Nullable<T>
object if it has been assigned a valid underlying value.
Here an example in LINQPad ;
DateTime? DateUpdated = DateTime.Now;
DateUpdated.Value.ToString("MMMM dd, yyyy").Dump();
Output will be;
January 14, 2014
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.