2

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)

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
sani
  • 230
  • 1
  • 3
  • 12
  • 1
    Use `TimeSpan` for times. Use `[DisplayFormat(DataFormatString = "...")]` for formatting dates –  Jun 27 '16 at 07:28

2 Answers2

3
        [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

Kami
  • 19,134
  • 4
  • 51
  • 63
2

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

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88