TJ Riceball TJ Riceball - 1 month ago 7
C# Question

How to save time only without the date using ASP.NET MVC 5 Data Annotation "DataType.Time?

My Model Class looks like this:

[DataType(DataType.Time)]
public DateTime Time {get; set;}


When I run it, I have a cool an HTML 5 Input Type in Time. When I save the data, the value in my database looks like this:

7/11/2014 1:00AM


It automatically saves the current date. I just wanna have the time saved. I'm trying to create a reservation system.

Answer

In that case you need to store that as a string. You cannot separate Time from Date using BCL (Base Class Library) of .Net. They cannot exist as independent entities.

However youcan have a workaround to save Time in a TimeSpan object using this Representing Time (not date and time) in C#

Comments