Show Time and data using Data annotation

拜拜、爱过 提交于 2019-12-13 15:17:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!