使用数据注释显示时间和数据

Show Time and data using Data annotation

我想知道时间值的最佳数据类型是什么,还有相同的数据注释吗?

我还想知道有没有一种方法可以使用数据注释来格式化日期时间数据类型以显示日期和时间(例如:dd-mm-yy HH:MM AM/PM

如果对象是 DateTime 类型,那么您可以简单地使用 .ToString() 方法以所需格式打印它,方法是将格式字符串作为参数传递给 .ToString()。考虑以下示例:

 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);

注意:您可以找到更多格式选项here,and a working example here

        [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

转到此 link 并使用您所需的日期时间从