views:

417

answers:

4

Is there is way in which one can represent a time only value in .NET without the date? For example, indicating the opening time of a shop?

TimeSpan indicates a range, whereas I only want to store a time value. Using DateTime to indicate this would result in new DateTime(1,1,1,8,30,0) which is not really desirable.

+10  A: 

You can use timespan

TimeSpan timeSpan = new TimeSpan(2, 14, 18);
Console.WriteLine(timeSpan.ToString());     // Displays "02:14:18".

[Edit]
Considering the other answers and the edit to the question, I would still use TimeSpan. No point in creating a new structure where an existing one from the framework suffice.
On these lines you would end up duplicating many native data types.

John G
Exactly. DateTime uses TimeSpan for exactly that purpose. Doc for DateTime.TimeSpan Property: "A TimeSpan that represents the fraction of the day that has elapsed since midnight."
Marcel J.
TimeSpan indicates an interval whereas the time I'm talking about is not an interval, but a single fixed point over a range of dates.
sduplooy
It may be used as a fixed point at well, and as you specified in the question, it is without date. After all you decide how to use these datatypes to your benifit.
John G
@John G: While it *can* be used to represent a fixed point, I agree with the OP - overloading the use of `TimeSpan` like this is somewhat ugly. It's the best that's available within the framework itself, but that's not the same as saying it's pleasant.
Jon Skeet
@MarcelJ: I take issue with that description of DateTime.Time. At 3am on March 28th in London, there will only have been 2 hours since midnight, for example. "Time of day" != "elapsed time since midnight." I realise that's MSDN's description rather than yours, but I'm just pointing out the problems with it.
Jon Skeet
You can treat the timepsan in such a scenario to be a GMT representation, again an effecive usage as required.
John G
@John G: Yes, you *can* do that. I just think it's pretty ugly.
Jon Skeet
Still IMHO is better than a new set of classes to duplicate natively available data structure.
John G
+2  A: 

If that empty Date really bugs you, you can also to create a simpler Time structure:

struct Time
{
    public int Hours   { get; set; }
    public int Minutes { get; set; }
    public int Seconds { get; set; }
    public override string ToString()
    {  
        return String.Format(
            "{0:00}:{1:00}:{2:00}",
            this.Hours, this.Minutes, this.Seconds);
    }
}

Or, why to bother: if you don't need to do any calculation with that information, just store it as String.

Rubens Farias
Hmmm... maybe... but why reinvent the wheel? If the language already has a class/structure (which C# and VB.NET do), then go with it. But I do understand where you are trying to go with your answer.
Kris Krause
How would this structure be any different from TimeSpan, this would just duplicate it in a way.
John G
Downvoting you due to the existance of `TimeSpan`, which already handles this, and in a significantly better way.
Noon Silk
@silky, I wrote this after reading first answer; OP said on question he doesn't wanted to use TimeSpan; I, personally, would opt to use a plain DateTime
Rubens Farias
Rubens: Fair enough; I'll remove the downvote via a useless edit.
Noon Silk
+5  A: 

As others have said, you can use a DateTime and ignore the date, or use a TimeSpan. Personally I'm not keen on either of these solutions, as neither type really reflects the concept you're trying to represent - I regard the date/time types in .NET as somewhat on the sparse side which is one of the reasons I started Noda Time. When it's actually finished, you'll be able to use the LocalTime type to represent a time of day. I'm not expecting it to be production ready for quite a long time, but we're always open to offers of assistance :)

One thing to consider: the time of day is not necessarily the length of time since midnight on the same day...

Jon Skeet
"[T]he time of day is not necessarily the length of time since midnight on the same day..." Is daylight savings time the only reason? Just curious why you left it indefinite.
Jason
@Jason: Daylight saving is the only reason I can think of offhand - ignoring leap seconds as irrelevant to most applications. I mostly left it that way to encourage others to think why that might be. I reckon it's a good thing for people to think a bit more deeply about dates/times than they currently do :)
Jon Skeet
LocalTime is exactly what I need to support my requirement.
sduplooy
@sduplooy: Fancy helping us port it from Joda Time then? :)
Jon Skeet
Sure, I think I'll start on LocalTime :)
sduplooy
A: 

If you don't want to use a DateTime or TimeSpan, and just want to store the time of day, you could just store the seconds since midnight in an Int32, or (if you don't even want seconds) the minutes since midnight would fit into an Int16. It would be trivial to write the few methods required to access the Hour, Minute and Second from such a value.

The only reason I can think of to avoid DateTime/TimeSpan would be if the size of the structure is critical.

(Of course, if you use a simple scheme like the above wrapped in a class, then it would also be trivial to replace the storage with a TimeSpan in future if you suddenly realise that would give you an advantage)

Jason Williams