Show time and data using data annotation

I wanted to know what is the best data type for a time value, and is there also an annotation for the same?

I would also like to know a way to format the datatype datetime is, using Data Annotations to display the date and time (for example: dd-mm-yy HH:MM AM/PM)

+2
source share
2 answers

If the object is of type DateTime, you can simply use the method .ToString()to print in the required format by passing the format string as an argument to .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);

.

+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

0

All Articles