6

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.

TJ Riceball
  • 240
  • 1
  • 2
  • 11

5 Answers5

3
[DataType(DataType.Time)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:HH:mm}")]
public DateTime Hour { get; set; }
2

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#

Community
  • 1
  • 1
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
  • They certainly *can* exist as independent entities. They happen not to in the BCL, but they do in databases, and do in other libraries too... – Jon Skeet Jul 11 '14 at 08:48
  • @JonSkeet: I am talking about .Net. – Nikhil Agrawal Jul 11 '14 at 08:49
  • Well again, that can be done in libraries in .NET - my [Noda Time](http://nodatime.org) library separates them. If you mean to say "The BCL doesn't provide separate time and date types" then that's what you should have said... which is very different from what you're actually said. – Jon Skeet Jul 11 '14 at 08:57
  • @JonSkeet Seeking feedback for my answer as well :) – Arijit Mukherjee Jul 11 '14 at 09:19
  • Well, I'd say that it doesn't really address the question, which is about *storing* the data... and if the OP is using an appropriate database, then they can use a `TIME` field. In terms of "within the C# code" I'd advocate using Noda Time, of course :) – Jon Skeet Jul 11 '14 at 09:39
0

Similar question gives a series of answers you might like, How do I represent a time only value in .NET? but the

However the overriding question is what type you should set the time to in your db, and this question Best way to store time (hh:mm) in a database gives more

The following will give you a string representation from your DateTime, but read the other questions and answers, and you might want to save in a format other than string to make it easier to evaluate queries...

DateTime.Now.ToString("t");
Community
  • 1
  • 1
dave heywood
  • 791
  • 8
  • 13
  • And to be generally sane. It's a shame we have no idea where the OP is storing the data. If it's in SQL Server 2008+, then a `TIME` field is the most appropriate... – Jon Skeet Jul 11 '14 at 08:58
0
[DisplayFormat(DataFormatString = "{0:t}")]
public DateTime Time {get; set;}

will display only the time

For reference Follow :

MSDN Reference

Arijit Mukherjee
  • 3,817
  • 2
  • 31
  • 51
-2

Just change the data type as as string in database then you can save your time.

user3494837
  • 155
  • 1
  • 3
  • 14
  • 1
    Using a string to store date/time values in a database is a really bad idea. Databases *do* (generally) have separate date and time types, and *that's* the better solution. – Jon Skeet Jul 11 '14 at 08:57