问题
I wanted to know whats is the best data type for time value only also is there a data annotation for the same ?
Also I wanted to know is there a way to format datatype datetime using Data annotation to display date and time(eg: dd-mm-yy HH:MM AM/PM
)
回答1:
If the object is of type DateTime then you can simply use .ToString()
method for print it in required format, by passing the format string as argument to the .ToString(). Consider the following example:
DateTime currentDate = DateTime.Now; // let it be 27-06-2016 13:06
string format = "dd-MM-yy HH:MM tt";
string formatedDateTime = currentDate.ToString(format, CultureInfo.InvariantCulture);
Note : You can find more format options here,and a working example here
回答2:
[DisplayName("Proposed commisioning Date")]
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString ={0:yyyy-MM-dd}")]
public DateTime CommisioningDate { set; get; }
https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
go to this link and use your required date time fromat
来源:https://stackoverflow.com/questions/38048132/show-time-and-data-using-data-annotation