C# DateTime: What “date” to use when I'm using just the “time”?












30















I'm using a DateTime in C# to display times. What date portion does everyone use when constructing a time?



E.g. the following is not valid because there is no zero-th month or zero-th day:



// 4:37:58 PM
DateTime time = new DateTime(0, 0, 0, 16, 47, 58);


Do I use COM's zero date?



// 4:37:58 PM
DateTime time = new DateTime(1899, 12, 30, 16, 47, 58);


Or perhaps SQL Server's?



//4:37:58 PM
DateTime time = new DateTime(1900, 1, 1, 16, 47, 58);


I realize it's arbitrary, since I'll be ignoring the date portions in code, but it would still be nice to be able to use:



DateTime duration = time2 - time1;




Answer



I think I like MinValue



 DateTime time = DateTime.MinValue.Date.Add(new TimeSpan(16, 47, 58));




Note: I can't use a TimeSpan, because that doesn't store times of the day. And the reason I know that is because there's no way to display its contents as a time.



Which is to say that TimeSpan records a span of time, not a time of day, e.g.:



TimeSpan t = new TimeSpan(16, 47, 58);
t.ToString();


returns a span of time in the format hours:minutes:seconds, e.g.:



16:47:58


rather than a time:



4:47:58 PM    (United States)
04:47:58 nm (South Africa)
4:47:58.MD (Albania)
16:47:58 (Algeria)
04:47:58 م (Bahrain)
PM 4:47:58 (Singapore)
下午 04:47:58 (Taiwan)
04:47:58 PM (Belize)
4:47:58 p.m. (New Zealand)
4:47:58 μμ (Greece)
16.47.58 (Italy)
오후 4:47:58 (Korea)
04:47:58 ب.ظ (Iran)
ਸ਼ਾਮ 04:47:58 (India)
04:47:58 p.m. (Argentina)
etc


In other words, there is a difference between a timespan, and a time. And also realize that TimeSpan doesn't provide a mechanism to convert a span of time into a time of day - and there is a reason for that.










share|improve this question




















  • 7





    You say that TimeSpan does not store the time of day, but yet, the DateTime structure has a property called TimeOfDay and the type is a TimeSpan.

    – Pierre-Alain Vigeant
    Apr 29 '10 at 15:25











  • Please consider editing out answer from the question.

    – Alexei Levenkov
    Oct 25 '16 at 16:37
















30















I'm using a DateTime in C# to display times. What date portion does everyone use when constructing a time?



E.g. the following is not valid because there is no zero-th month or zero-th day:



// 4:37:58 PM
DateTime time = new DateTime(0, 0, 0, 16, 47, 58);


Do I use COM's zero date?



// 4:37:58 PM
DateTime time = new DateTime(1899, 12, 30, 16, 47, 58);


Or perhaps SQL Server's?



//4:37:58 PM
DateTime time = new DateTime(1900, 1, 1, 16, 47, 58);


I realize it's arbitrary, since I'll be ignoring the date portions in code, but it would still be nice to be able to use:



DateTime duration = time2 - time1;




Answer



I think I like MinValue



 DateTime time = DateTime.MinValue.Date.Add(new TimeSpan(16, 47, 58));




Note: I can't use a TimeSpan, because that doesn't store times of the day. And the reason I know that is because there's no way to display its contents as a time.



Which is to say that TimeSpan records a span of time, not a time of day, e.g.:



TimeSpan t = new TimeSpan(16, 47, 58);
t.ToString();


returns a span of time in the format hours:minutes:seconds, e.g.:



16:47:58


rather than a time:



4:47:58 PM    (United States)
04:47:58 nm (South Africa)
4:47:58.MD (Albania)
16:47:58 (Algeria)
04:47:58 م (Bahrain)
PM 4:47:58 (Singapore)
下午 04:47:58 (Taiwan)
04:47:58 PM (Belize)
4:47:58 p.m. (New Zealand)
4:47:58 μμ (Greece)
16.47.58 (Italy)
오후 4:47:58 (Korea)
04:47:58 ب.ظ (Iran)
ਸ਼ਾਮ 04:47:58 (India)
04:47:58 p.m. (Argentina)
etc


In other words, there is a difference between a timespan, and a time. And also realize that TimeSpan doesn't provide a mechanism to convert a span of time into a time of day - and there is a reason for that.










share|improve this question




















  • 7





    You say that TimeSpan does not store the time of day, but yet, the DateTime structure has a property called TimeOfDay and the type is a TimeSpan.

    – Pierre-Alain Vigeant
    Apr 29 '10 at 15:25











  • Please consider editing out answer from the question.

    – Alexei Levenkov
    Oct 25 '16 at 16:37














30












30








30


2






I'm using a DateTime in C# to display times. What date portion does everyone use when constructing a time?



E.g. the following is not valid because there is no zero-th month or zero-th day:



// 4:37:58 PM
DateTime time = new DateTime(0, 0, 0, 16, 47, 58);


Do I use COM's zero date?



// 4:37:58 PM
DateTime time = new DateTime(1899, 12, 30, 16, 47, 58);


Or perhaps SQL Server's?



//4:37:58 PM
DateTime time = new DateTime(1900, 1, 1, 16, 47, 58);


I realize it's arbitrary, since I'll be ignoring the date portions in code, but it would still be nice to be able to use:



DateTime duration = time2 - time1;




Answer



I think I like MinValue



 DateTime time = DateTime.MinValue.Date.Add(new TimeSpan(16, 47, 58));




Note: I can't use a TimeSpan, because that doesn't store times of the day. And the reason I know that is because there's no way to display its contents as a time.



Which is to say that TimeSpan records a span of time, not a time of day, e.g.:



TimeSpan t = new TimeSpan(16, 47, 58);
t.ToString();


returns a span of time in the format hours:minutes:seconds, e.g.:



16:47:58


rather than a time:



4:47:58 PM    (United States)
04:47:58 nm (South Africa)
4:47:58.MD (Albania)
16:47:58 (Algeria)
04:47:58 م (Bahrain)
PM 4:47:58 (Singapore)
下午 04:47:58 (Taiwan)
04:47:58 PM (Belize)
4:47:58 p.m. (New Zealand)
4:47:58 μμ (Greece)
16.47.58 (Italy)
오후 4:47:58 (Korea)
04:47:58 ب.ظ (Iran)
ਸ਼ਾਮ 04:47:58 (India)
04:47:58 p.m. (Argentina)
etc


In other words, there is a difference between a timespan, and a time. And also realize that TimeSpan doesn't provide a mechanism to convert a span of time into a time of day - and there is a reason for that.










share|improve this question
















I'm using a DateTime in C# to display times. What date portion does everyone use when constructing a time?



E.g. the following is not valid because there is no zero-th month or zero-th day:



// 4:37:58 PM
DateTime time = new DateTime(0, 0, 0, 16, 47, 58);


Do I use COM's zero date?



// 4:37:58 PM
DateTime time = new DateTime(1899, 12, 30, 16, 47, 58);


Or perhaps SQL Server's?



//4:37:58 PM
DateTime time = new DateTime(1900, 1, 1, 16, 47, 58);


I realize it's arbitrary, since I'll be ignoring the date portions in code, but it would still be nice to be able to use:



DateTime duration = time2 - time1;




Answer



I think I like MinValue



 DateTime time = DateTime.MinValue.Date.Add(new TimeSpan(16, 47, 58));




Note: I can't use a TimeSpan, because that doesn't store times of the day. And the reason I know that is because there's no way to display its contents as a time.



Which is to say that TimeSpan records a span of time, not a time of day, e.g.:



TimeSpan t = new TimeSpan(16, 47, 58);
t.ToString();


returns a span of time in the format hours:minutes:seconds, e.g.:



16:47:58


rather than a time:



4:47:58 PM    (United States)
04:47:58 nm (South Africa)
4:47:58.MD (Albania)
16:47:58 (Algeria)
04:47:58 م (Bahrain)
PM 4:47:58 (Singapore)
下午 04:47:58 (Taiwan)
04:47:58 PM (Belize)
4:47:58 p.m. (New Zealand)
4:47:58 μμ (Greece)
16.47.58 (Italy)
오후 4:47:58 (Korea)
04:47:58 ب.ظ (Iran)
ਸ਼ਾਮ 04:47:58 (India)
04:47:58 p.m. (Argentina)
etc


In other words, there is a difference between a timespan, and a time. And also realize that TimeSpan doesn't provide a mechanism to convert a span of time into a time of day - and there is a reason for that.







c# datetime time






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 14:42









Ɖiamond ǤeezeƦ

2,65532136




2,65532136










asked Dec 16 '08 at 20:35









Ian BoydIan Boyd

119k1866791008




119k1866791008








  • 7





    You say that TimeSpan does not store the time of day, but yet, the DateTime structure has a property called TimeOfDay and the type is a TimeSpan.

    – Pierre-Alain Vigeant
    Apr 29 '10 at 15:25











  • Please consider editing out answer from the question.

    – Alexei Levenkov
    Oct 25 '16 at 16:37














  • 7





    You say that TimeSpan does not store the time of day, but yet, the DateTime structure has a property called TimeOfDay and the type is a TimeSpan.

    – Pierre-Alain Vigeant
    Apr 29 '10 at 15:25











  • Please consider editing out answer from the question.

    – Alexei Levenkov
    Oct 25 '16 at 16:37








7




7





You say that TimeSpan does not store the time of day, but yet, the DateTime structure has a property called TimeOfDay and the type is a TimeSpan.

– Pierre-Alain Vigeant
Apr 29 '10 at 15:25





You say that TimeSpan does not store the time of day, but yet, the DateTime structure has a property called TimeOfDay and the type is a TimeSpan.

– Pierre-Alain Vigeant
Apr 29 '10 at 15:25













Please consider editing out answer from the question.

– Alexei Levenkov
Oct 25 '16 at 16:37





Please consider editing out answer from the question.

– Alexei Levenkov
Oct 25 '16 at 16:37












12 Answers
12






active

oldest

votes


















25














what about DateTime.MinValue?






share|improve this answer
























  • i like that the best, and not the hard-coded numbers, but add a timespan to the MinValue's Date.

    – Ian Boyd
    Dec 16 '08 at 21:38



















8














Let's help out the guys who want a Time structure:



/// <summary>
/// Time structure
/// </summary>
public struct Time : IComparable
{
private int minuteOfDay;
public static Time Midnight = "0:00";
private static int MIN_OF_DAY = 60 * 24;

public Time(int minuteOfDay)
{
if (minuteOfDay >= (60 * 24) || minuteOfDay < 0)
throw new ArgumentException("Must be in the range 0-1439", "minuteOfDay");
this.minuteOfDay = minuteOfDay;
}

public Time(int hour, int minutes)
{
if (hour < 0 || hour > 23)
throw new ArgumentException("Must be in the range 0-23", "hour");
if (minutes < 0 || minutes > 59)
throw new ArgumentException("Must be in the range 0-59", "minutes");

minuteOfDay = (hour * 60) + minutes;
}

#region Operators
public static implicit operator Time(string s)
{
var parts = s.Split(':');
if (parts.Length != 2)
throw new ArgumentException("Time must be specified on the form tt:mm");
return new Time(int.Parse(parts[0]), int.Parse(parts[1]));
}


public static bool operator >(Time t1, Time t2)
{
return t1.MinuteOfDay > t2.MinuteOfDay;
}
public static bool operator <(Time t1, Time t2)
{
return t1.MinuteOfDay < t2.MinuteOfDay;
}
public static bool operator >=(Time t1, Time t2)
{
return t1.MinuteOfDay >= t2.MinuteOfDay;
}
public static bool operator <=(Time t1, Time t2)
{
return t1.MinuteOfDay <= t2.MinuteOfDay;
}
public static bool operator ==(Time t1, Time t2)
{
return t1.GetHashCode() == t2.GetHashCode();
}
public static bool operator !=(Time t1, Time t2)
{
return t1.GetHashCode() != t2.GetHashCode();
}

/// Time
/// Minutes that remain to
/// Time conferred minutes
public static Time operator +(Time t, int min)
{
if (t.minuteOfDay + min < (24 * 60))
{
t.minuteOfDay += min;
return t;
}
else
{
t.minuteOfDay = (t.minuteOfDay + min) % MIN_OF_DAY;
return t;
}
}

public static Time operator -(Time t, int min)
{
if (t.minuteOfDay - min > -1)
{
t.minuteOfDay -= min;
return t;
}
else
{
t.minuteOfDay = MIN_OF_DAY + (t.minuteOfDay - min);
return t;
}
}

public static TimeSpan operator -(Time t1, Time t2)
{
return TimeSpan.FromMinutes(Time.Span(t2, t1));
}
#endregion


public int Hour
{
get
{
return (int)(minuteOfDay / 60);
}
}
public int Minutes
{
get
{
return minuteOfDay % 60;
}
}


public int MinuteOfDay
{
get { return minuteOfDay; }
}

public Time AddHours(int hours)
{
return this + (hours * 60);
}

public int CompareTo(Time other)
{
return this.minuteOfDay.CompareTo(other.minuteOfDay);
}

#region Overrides
public override int GetHashCode()
{
return minuteOfDay.GetHashCode();
}

public override string ToString()
{
return string.Format("{0}:{1:00}", Hour, Minutes);
}
#endregion

///
/// Safe enumerering - whatever interval applied max days
///
/// Start time
/// Spring in minutes
///
public static IEnumerable Range(Time start, int step)
{
return Range(start, start, step);
}

///
/// Safe enumeration - whatever interval applied max days
///
public static IEnumerable Range(Time start, Time stop, int step)
{
int offset = start.MinuteOfDay;
for (var i = 0; i < Time.Span(start, stop); i += step)
{
yield return Time.Midnight + (i + offset);
}
}

///
/// Calculates the number of minutes between t1 and t2
///
public static int Span(Time t1, Time t2)
{
if (t1 < t2) // same day
return t2.MinuteOfDay - t1.MinuteOfDay;
else // over midnight
return MIN_OF_DAY - t1.MinuteOfDay + t2.MinuteOfDay;
}
}





share|improve this answer


























  • sorry the bad translation from the danish language!

    – Junior M
    May 18 '09 at 23:08






  • 7





    Uh, a Time structure with a resolution of one minute? Give me a break...

    – Lucero
    Nov 20 '10 at 19:35



















6














A TimeSpan most certainly can store the time of the day - you just have to treat the value as the amount of time elapsed since midnight, basically the same way we read a clock.






share|improve this answer
























  • TimeSpan does not store a time of day. Updated question to show you why.

    – Ian Boyd
    Dec 16 '08 at 21:18











  • Just store it as UTC :)

    – Greg Hurlman
    Dec 17 '08 at 15:45



















6














Personally I'd create a custom Time struct that contains a DateTime instance, and which has similar properties, constructors etc. but doesn't expose days/months/etc. Just make all your public accessors pass through to the contained instance. Then you can simply have the epoch as a private static readonly DateTime field and it doesn't matter what value you choose as it's all neatly contained within your custom struct. In the rest of your code can simply write:



var time = new Time(16, 47, 58);





share|improve this answer































    4














    Given that DateTime.TimeOfDay returns a TimeSpan, I'd use that.



    Why can't you use a TimeSpan? I don't understand your comment that it doesn't store times of day.






    share|improve this answer































      3














      How about DateTime.Now.TimeOfDay, and use the TimeSpan?



      Re "because that doesn't store times of the day." - well, it does if you think of a TimeSpan as the time since midnight.



      A "duration", for example, screams TimeSpan.






      share|improve this answer
























      • Subtracting two DateTimes yields a TimeSpan.

        – Ian Boyd
        Dec 16 '08 at 21:19



















      3














      To display a TimeSpan formatted with local culture, simply add it to a date like DateTime.Today. Something like this:



      (DateTime.Today + timeSpan).ToString();



      Since your value really doesn't represent a date, you're better off storing it as a TimeSpan until the time comes to display it.






      share|improve this answer































        1














        I recommend DateTime.MinValue






        share|improve this answer































          1














          You can just create a new DateTime with a string literal.



          String literal for time:



          DateTime t = new DateTime("01:00:30");


          String literal for date:



          DateTime t = new DateTime("01/05/2008"); // english format
          DateTime t = new DateTime("05.01.2008"); // german format


          For a DateTime with date and time values:



          DateTime t = new DateTime("01/05/2008T01:00:30");


          In most cases, when creating a DateTime, i set it to DateTime.Now, if it is not actually set to anything else. If you instantiate an DateTime manually, you should beware of the DateTimeKind set correctly, otherwise this could lead to surprises.






          share|improve this answer































            1














            May I suggest that in some cases a custom struct could do? It could have an Int32 backing value (there are 86 milion milliseconds in a day; this would fit in an Int32).



            There could be get-only properties :



            Hours
            Minutes
            Seconds
            Milliseconds



            You could also overload operators such as +, - and so on. Implement IEquatable, IComparable and whatever may be the case. Overload Equals, == . Overload and override ToString.



            You could also provide more methods to construct from a DateTime or append to a datetime and so on.






            share|improve this answer































              0














              Use a TimeSpan, and make it UTC if you have TimeZone issues.






              share|improve this answer































                0














                No much difference compare to the accepted answer. Just to implement the idea.



                public class TimeOfDay
                {
                public DateTime time;
                public TimeOfDay(int Hour, int Minute, int Second)
                {
                time = DateTime.MinValue.Date.Add(new TimeSpan(Hour, Minute, Second));
                }
                }





                share|improve this answer























                  Your Answer






                  StackExchange.ifUsing("editor", function () {
                  StackExchange.using("externalEditor", function () {
                  StackExchange.using("snippets", function () {
                  StackExchange.snippets.init();
                  });
                  });
                  }, "code-snippets");

                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "1"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f372601%2fc-sharp-datetime-what-date-to-use-when-im-using-just-the-time%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  12 Answers
                  12






                  active

                  oldest

                  votes








                  12 Answers
                  12






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  25














                  what about DateTime.MinValue?






                  share|improve this answer
























                  • i like that the best, and not the hard-coded numbers, but add a timespan to the MinValue's Date.

                    – Ian Boyd
                    Dec 16 '08 at 21:38
















                  25














                  what about DateTime.MinValue?






                  share|improve this answer
























                  • i like that the best, and not the hard-coded numbers, but add a timespan to the MinValue's Date.

                    – Ian Boyd
                    Dec 16 '08 at 21:38














                  25












                  25








                  25







                  what about DateTime.MinValue?






                  share|improve this answer













                  what about DateTime.MinValue?







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 16 '08 at 20:40









                  Joachim KerschbaumerJoachim Kerschbaumer

                  7,53674181




                  7,53674181













                  • i like that the best, and not the hard-coded numbers, but add a timespan to the MinValue's Date.

                    – Ian Boyd
                    Dec 16 '08 at 21:38



















                  • i like that the best, and not the hard-coded numbers, but add a timespan to the MinValue's Date.

                    – Ian Boyd
                    Dec 16 '08 at 21:38

















                  i like that the best, and not the hard-coded numbers, but add a timespan to the MinValue's Date.

                  – Ian Boyd
                  Dec 16 '08 at 21:38





                  i like that the best, and not the hard-coded numbers, but add a timespan to the MinValue's Date.

                  – Ian Boyd
                  Dec 16 '08 at 21:38













                  8














                  Let's help out the guys who want a Time structure:



                  /// <summary>
                  /// Time structure
                  /// </summary>
                  public struct Time : IComparable
                  {
                  private int minuteOfDay;
                  public static Time Midnight = "0:00";
                  private static int MIN_OF_DAY = 60 * 24;

                  public Time(int minuteOfDay)
                  {
                  if (minuteOfDay >= (60 * 24) || minuteOfDay < 0)
                  throw new ArgumentException("Must be in the range 0-1439", "minuteOfDay");
                  this.minuteOfDay = minuteOfDay;
                  }

                  public Time(int hour, int minutes)
                  {
                  if (hour < 0 || hour > 23)
                  throw new ArgumentException("Must be in the range 0-23", "hour");
                  if (minutes < 0 || minutes > 59)
                  throw new ArgumentException("Must be in the range 0-59", "minutes");

                  minuteOfDay = (hour * 60) + minutes;
                  }

                  #region Operators
                  public static implicit operator Time(string s)
                  {
                  var parts = s.Split(':');
                  if (parts.Length != 2)
                  throw new ArgumentException("Time must be specified on the form tt:mm");
                  return new Time(int.Parse(parts[0]), int.Parse(parts[1]));
                  }


                  public static bool operator >(Time t1, Time t2)
                  {
                  return t1.MinuteOfDay > t2.MinuteOfDay;
                  }
                  public static bool operator <(Time t1, Time t2)
                  {
                  return t1.MinuteOfDay < t2.MinuteOfDay;
                  }
                  public static bool operator >=(Time t1, Time t2)
                  {
                  return t1.MinuteOfDay >= t2.MinuteOfDay;
                  }
                  public static bool operator <=(Time t1, Time t2)
                  {
                  return t1.MinuteOfDay <= t2.MinuteOfDay;
                  }
                  public static bool operator ==(Time t1, Time t2)
                  {
                  return t1.GetHashCode() == t2.GetHashCode();
                  }
                  public static bool operator !=(Time t1, Time t2)
                  {
                  return t1.GetHashCode() != t2.GetHashCode();
                  }

                  /// Time
                  /// Minutes that remain to
                  /// Time conferred minutes
                  public static Time operator +(Time t, int min)
                  {
                  if (t.minuteOfDay + min < (24 * 60))
                  {
                  t.minuteOfDay += min;
                  return t;
                  }
                  else
                  {
                  t.minuteOfDay = (t.minuteOfDay + min) % MIN_OF_DAY;
                  return t;
                  }
                  }

                  public static Time operator -(Time t, int min)
                  {
                  if (t.minuteOfDay - min > -1)
                  {
                  t.minuteOfDay -= min;
                  return t;
                  }
                  else
                  {
                  t.minuteOfDay = MIN_OF_DAY + (t.minuteOfDay - min);
                  return t;
                  }
                  }

                  public static TimeSpan operator -(Time t1, Time t2)
                  {
                  return TimeSpan.FromMinutes(Time.Span(t2, t1));
                  }
                  #endregion


                  public int Hour
                  {
                  get
                  {
                  return (int)(minuteOfDay / 60);
                  }
                  }
                  public int Minutes
                  {
                  get
                  {
                  return minuteOfDay % 60;
                  }
                  }


                  public int MinuteOfDay
                  {
                  get { return minuteOfDay; }
                  }

                  public Time AddHours(int hours)
                  {
                  return this + (hours * 60);
                  }

                  public int CompareTo(Time other)
                  {
                  return this.minuteOfDay.CompareTo(other.minuteOfDay);
                  }

                  #region Overrides
                  public override int GetHashCode()
                  {
                  return minuteOfDay.GetHashCode();
                  }

                  public override string ToString()
                  {
                  return string.Format("{0}:{1:00}", Hour, Minutes);
                  }
                  #endregion

                  ///
                  /// Safe enumerering - whatever interval applied max days
                  ///
                  /// Start time
                  /// Spring in minutes
                  ///
                  public static IEnumerable Range(Time start, int step)
                  {
                  return Range(start, start, step);
                  }

                  ///
                  /// Safe enumeration - whatever interval applied max days
                  ///
                  public static IEnumerable Range(Time start, Time stop, int step)
                  {
                  int offset = start.MinuteOfDay;
                  for (var i = 0; i < Time.Span(start, stop); i += step)
                  {
                  yield return Time.Midnight + (i + offset);
                  }
                  }

                  ///
                  /// Calculates the number of minutes between t1 and t2
                  ///
                  public static int Span(Time t1, Time t2)
                  {
                  if (t1 < t2) // same day
                  return t2.MinuteOfDay - t1.MinuteOfDay;
                  else // over midnight
                  return MIN_OF_DAY - t1.MinuteOfDay + t2.MinuteOfDay;
                  }
                  }





                  share|improve this answer


























                  • sorry the bad translation from the danish language!

                    – Junior M
                    May 18 '09 at 23:08






                  • 7





                    Uh, a Time structure with a resolution of one minute? Give me a break...

                    – Lucero
                    Nov 20 '10 at 19:35
















                  8














                  Let's help out the guys who want a Time structure:



                  /// <summary>
                  /// Time structure
                  /// </summary>
                  public struct Time : IComparable
                  {
                  private int minuteOfDay;
                  public static Time Midnight = "0:00";
                  private static int MIN_OF_DAY = 60 * 24;

                  public Time(int minuteOfDay)
                  {
                  if (minuteOfDay >= (60 * 24) || minuteOfDay < 0)
                  throw new ArgumentException("Must be in the range 0-1439", "minuteOfDay");
                  this.minuteOfDay = minuteOfDay;
                  }

                  public Time(int hour, int minutes)
                  {
                  if (hour < 0 || hour > 23)
                  throw new ArgumentException("Must be in the range 0-23", "hour");
                  if (minutes < 0 || minutes > 59)
                  throw new ArgumentException("Must be in the range 0-59", "minutes");

                  minuteOfDay = (hour * 60) + minutes;
                  }

                  #region Operators
                  public static implicit operator Time(string s)
                  {
                  var parts = s.Split(':');
                  if (parts.Length != 2)
                  throw new ArgumentException("Time must be specified on the form tt:mm");
                  return new Time(int.Parse(parts[0]), int.Parse(parts[1]));
                  }


                  public static bool operator >(Time t1, Time t2)
                  {
                  return t1.MinuteOfDay > t2.MinuteOfDay;
                  }
                  public static bool operator <(Time t1, Time t2)
                  {
                  return t1.MinuteOfDay < t2.MinuteOfDay;
                  }
                  public static bool operator >=(Time t1, Time t2)
                  {
                  return t1.MinuteOfDay >= t2.MinuteOfDay;
                  }
                  public static bool operator <=(Time t1, Time t2)
                  {
                  return t1.MinuteOfDay <= t2.MinuteOfDay;
                  }
                  public static bool operator ==(Time t1, Time t2)
                  {
                  return t1.GetHashCode() == t2.GetHashCode();
                  }
                  public static bool operator !=(Time t1, Time t2)
                  {
                  return t1.GetHashCode() != t2.GetHashCode();
                  }

                  /// Time
                  /// Minutes that remain to
                  /// Time conferred minutes
                  public static Time operator +(Time t, int min)
                  {
                  if (t.minuteOfDay + min < (24 * 60))
                  {
                  t.minuteOfDay += min;
                  return t;
                  }
                  else
                  {
                  t.minuteOfDay = (t.minuteOfDay + min) % MIN_OF_DAY;
                  return t;
                  }
                  }

                  public static Time operator -(Time t, int min)
                  {
                  if (t.minuteOfDay - min > -1)
                  {
                  t.minuteOfDay -= min;
                  return t;
                  }
                  else
                  {
                  t.minuteOfDay = MIN_OF_DAY + (t.minuteOfDay - min);
                  return t;
                  }
                  }

                  public static TimeSpan operator -(Time t1, Time t2)
                  {
                  return TimeSpan.FromMinutes(Time.Span(t2, t1));
                  }
                  #endregion


                  public int Hour
                  {
                  get
                  {
                  return (int)(minuteOfDay / 60);
                  }
                  }
                  public int Minutes
                  {
                  get
                  {
                  return minuteOfDay % 60;
                  }
                  }


                  public int MinuteOfDay
                  {
                  get { return minuteOfDay; }
                  }

                  public Time AddHours(int hours)
                  {
                  return this + (hours * 60);
                  }

                  public int CompareTo(Time other)
                  {
                  return this.minuteOfDay.CompareTo(other.minuteOfDay);
                  }

                  #region Overrides
                  public override int GetHashCode()
                  {
                  return minuteOfDay.GetHashCode();
                  }

                  public override string ToString()
                  {
                  return string.Format("{0}:{1:00}", Hour, Minutes);
                  }
                  #endregion

                  ///
                  /// Safe enumerering - whatever interval applied max days
                  ///
                  /// Start time
                  /// Spring in minutes
                  ///
                  public static IEnumerable Range(Time start, int step)
                  {
                  return Range(start, start, step);
                  }

                  ///
                  /// Safe enumeration - whatever interval applied max days
                  ///
                  public static IEnumerable Range(Time start, Time stop, int step)
                  {
                  int offset = start.MinuteOfDay;
                  for (var i = 0; i < Time.Span(start, stop); i += step)
                  {
                  yield return Time.Midnight + (i + offset);
                  }
                  }

                  ///
                  /// Calculates the number of minutes between t1 and t2
                  ///
                  public static int Span(Time t1, Time t2)
                  {
                  if (t1 < t2) // same day
                  return t2.MinuteOfDay - t1.MinuteOfDay;
                  else // over midnight
                  return MIN_OF_DAY - t1.MinuteOfDay + t2.MinuteOfDay;
                  }
                  }





                  share|improve this answer


























                  • sorry the bad translation from the danish language!

                    – Junior M
                    May 18 '09 at 23:08






                  • 7





                    Uh, a Time structure with a resolution of one minute? Give me a break...

                    – Lucero
                    Nov 20 '10 at 19:35














                  8












                  8








                  8







                  Let's help out the guys who want a Time structure:



                  /// <summary>
                  /// Time structure
                  /// </summary>
                  public struct Time : IComparable
                  {
                  private int minuteOfDay;
                  public static Time Midnight = "0:00";
                  private static int MIN_OF_DAY = 60 * 24;

                  public Time(int minuteOfDay)
                  {
                  if (minuteOfDay >= (60 * 24) || minuteOfDay < 0)
                  throw new ArgumentException("Must be in the range 0-1439", "minuteOfDay");
                  this.minuteOfDay = minuteOfDay;
                  }

                  public Time(int hour, int minutes)
                  {
                  if (hour < 0 || hour > 23)
                  throw new ArgumentException("Must be in the range 0-23", "hour");
                  if (minutes < 0 || minutes > 59)
                  throw new ArgumentException("Must be in the range 0-59", "minutes");

                  minuteOfDay = (hour * 60) + minutes;
                  }

                  #region Operators
                  public static implicit operator Time(string s)
                  {
                  var parts = s.Split(':');
                  if (parts.Length != 2)
                  throw new ArgumentException("Time must be specified on the form tt:mm");
                  return new Time(int.Parse(parts[0]), int.Parse(parts[1]));
                  }


                  public static bool operator >(Time t1, Time t2)
                  {
                  return t1.MinuteOfDay > t2.MinuteOfDay;
                  }
                  public static bool operator <(Time t1, Time t2)
                  {
                  return t1.MinuteOfDay < t2.MinuteOfDay;
                  }
                  public static bool operator >=(Time t1, Time t2)
                  {
                  return t1.MinuteOfDay >= t2.MinuteOfDay;
                  }
                  public static bool operator <=(Time t1, Time t2)
                  {
                  return t1.MinuteOfDay <= t2.MinuteOfDay;
                  }
                  public static bool operator ==(Time t1, Time t2)
                  {
                  return t1.GetHashCode() == t2.GetHashCode();
                  }
                  public static bool operator !=(Time t1, Time t2)
                  {
                  return t1.GetHashCode() != t2.GetHashCode();
                  }

                  /// Time
                  /// Minutes that remain to
                  /// Time conferred minutes
                  public static Time operator +(Time t, int min)
                  {
                  if (t.minuteOfDay + min < (24 * 60))
                  {
                  t.minuteOfDay += min;
                  return t;
                  }
                  else
                  {
                  t.minuteOfDay = (t.minuteOfDay + min) % MIN_OF_DAY;
                  return t;
                  }
                  }

                  public static Time operator -(Time t, int min)
                  {
                  if (t.minuteOfDay - min > -1)
                  {
                  t.minuteOfDay -= min;
                  return t;
                  }
                  else
                  {
                  t.minuteOfDay = MIN_OF_DAY + (t.minuteOfDay - min);
                  return t;
                  }
                  }

                  public static TimeSpan operator -(Time t1, Time t2)
                  {
                  return TimeSpan.FromMinutes(Time.Span(t2, t1));
                  }
                  #endregion


                  public int Hour
                  {
                  get
                  {
                  return (int)(minuteOfDay / 60);
                  }
                  }
                  public int Minutes
                  {
                  get
                  {
                  return minuteOfDay % 60;
                  }
                  }


                  public int MinuteOfDay
                  {
                  get { return minuteOfDay; }
                  }

                  public Time AddHours(int hours)
                  {
                  return this + (hours * 60);
                  }

                  public int CompareTo(Time other)
                  {
                  return this.minuteOfDay.CompareTo(other.minuteOfDay);
                  }

                  #region Overrides
                  public override int GetHashCode()
                  {
                  return minuteOfDay.GetHashCode();
                  }

                  public override string ToString()
                  {
                  return string.Format("{0}:{1:00}", Hour, Minutes);
                  }
                  #endregion

                  ///
                  /// Safe enumerering - whatever interval applied max days
                  ///
                  /// Start time
                  /// Spring in minutes
                  ///
                  public static IEnumerable Range(Time start, int step)
                  {
                  return Range(start, start, step);
                  }

                  ///
                  /// Safe enumeration - whatever interval applied max days
                  ///
                  public static IEnumerable Range(Time start, Time stop, int step)
                  {
                  int offset = start.MinuteOfDay;
                  for (var i = 0; i < Time.Span(start, stop); i += step)
                  {
                  yield return Time.Midnight + (i + offset);
                  }
                  }

                  ///
                  /// Calculates the number of minutes between t1 and t2
                  ///
                  public static int Span(Time t1, Time t2)
                  {
                  if (t1 < t2) // same day
                  return t2.MinuteOfDay - t1.MinuteOfDay;
                  else // over midnight
                  return MIN_OF_DAY - t1.MinuteOfDay + t2.MinuteOfDay;
                  }
                  }





                  share|improve this answer















                  Let's help out the guys who want a Time structure:



                  /// <summary>
                  /// Time structure
                  /// </summary>
                  public struct Time : IComparable
                  {
                  private int minuteOfDay;
                  public static Time Midnight = "0:00";
                  private static int MIN_OF_DAY = 60 * 24;

                  public Time(int minuteOfDay)
                  {
                  if (minuteOfDay >= (60 * 24) || minuteOfDay < 0)
                  throw new ArgumentException("Must be in the range 0-1439", "minuteOfDay");
                  this.minuteOfDay = minuteOfDay;
                  }

                  public Time(int hour, int minutes)
                  {
                  if (hour < 0 || hour > 23)
                  throw new ArgumentException("Must be in the range 0-23", "hour");
                  if (minutes < 0 || minutes > 59)
                  throw new ArgumentException("Must be in the range 0-59", "minutes");

                  minuteOfDay = (hour * 60) + minutes;
                  }

                  #region Operators
                  public static implicit operator Time(string s)
                  {
                  var parts = s.Split(':');
                  if (parts.Length != 2)
                  throw new ArgumentException("Time must be specified on the form tt:mm");
                  return new Time(int.Parse(parts[0]), int.Parse(parts[1]));
                  }


                  public static bool operator >(Time t1, Time t2)
                  {
                  return t1.MinuteOfDay > t2.MinuteOfDay;
                  }
                  public static bool operator <(Time t1, Time t2)
                  {
                  return t1.MinuteOfDay < t2.MinuteOfDay;
                  }
                  public static bool operator >=(Time t1, Time t2)
                  {
                  return t1.MinuteOfDay >= t2.MinuteOfDay;
                  }
                  public static bool operator <=(Time t1, Time t2)
                  {
                  return t1.MinuteOfDay <= t2.MinuteOfDay;
                  }
                  public static bool operator ==(Time t1, Time t2)
                  {
                  return t1.GetHashCode() == t2.GetHashCode();
                  }
                  public static bool operator !=(Time t1, Time t2)
                  {
                  return t1.GetHashCode() != t2.GetHashCode();
                  }

                  /// Time
                  /// Minutes that remain to
                  /// Time conferred minutes
                  public static Time operator +(Time t, int min)
                  {
                  if (t.minuteOfDay + min < (24 * 60))
                  {
                  t.minuteOfDay += min;
                  return t;
                  }
                  else
                  {
                  t.minuteOfDay = (t.minuteOfDay + min) % MIN_OF_DAY;
                  return t;
                  }
                  }

                  public static Time operator -(Time t, int min)
                  {
                  if (t.minuteOfDay - min > -1)
                  {
                  t.minuteOfDay -= min;
                  return t;
                  }
                  else
                  {
                  t.minuteOfDay = MIN_OF_DAY + (t.minuteOfDay - min);
                  return t;
                  }
                  }

                  public static TimeSpan operator -(Time t1, Time t2)
                  {
                  return TimeSpan.FromMinutes(Time.Span(t2, t1));
                  }
                  #endregion


                  public int Hour
                  {
                  get
                  {
                  return (int)(minuteOfDay / 60);
                  }
                  }
                  public int Minutes
                  {
                  get
                  {
                  return minuteOfDay % 60;
                  }
                  }


                  public int MinuteOfDay
                  {
                  get { return minuteOfDay; }
                  }

                  public Time AddHours(int hours)
                  {
                  return this + (hours * 60);
                  }

                  public int CompareTo(Time other)
                  {
                  return this.minuteOfDay.CompareTo(other.minuteOfDay);
                  }

                  #region Overrides
                  public override int GetHashCode()
                  {
                  return minuteOfDay.GetHashCode();
                  }

                  public override string ToString()
                  {
                  return string.Format("{0}:{1:00}", Hour, Minutes);
                  }
                  #endregion

                  ///
                  /// Safe enumerering - whatever interval applied max days
                  ///
                  /// Start time
                  /// Spring in minutes
                  ///
                  public static IEnumerable Range(Time start, int step)
                  {
                  return Range(start, start, step);
                  }

                  ///
                  /// Safe enumeration - whatever interval applied max days
                  ///
                  public static IEnumerable Range(Time start, Time stop, int step)
                  {
                  int offset = start.MinuteOfDay;
                  for (var i = 0; i < Time.Span(start, stop); i += step)
                  {
                  yield return Time.Midnight + (i + offset);
                  }
                  }

                  ///
                  /// Calculates the number of minutes between t1 and t2
                  ///
                  public static int Span(Time t1, Time t2)
                  {
                  if (t1 < t2) // same day
                  return t2.MinuteOfDay - t1.MinuteOfDay;
                  else // over midnight
                  return MIN_OF_DAY - t1.MinuteOfDay + t2.MinuteOfDay;
                  }
                  }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 14 '12 at 0:57









                  Owen Blacker

                  3,10312666




                  3,10312666










                  answered May 18 '09 at 23:07









                  Junior MJunior M

                  9,9702497153




                  9,9702497153













                  • sorry the bad translation from the danish language!

                    – Junior M
                    May 18 '09 at 23:08






                  • 7





                    Uh, a Time structure with a resolution of one minute? Give me a break...

                    – Lucero
                    Nov 20 '10 at 19:35



















                  • sorry the bad translation from the danish language!

                    – Junior M
                    May 18 '09 at 23:08






                  • 7





                    Uh, a Time structure with a resolution of one minute? Give me a break...

                    – Lucero
                    Nov 20 '10 at 19:35

















                  sorry the bad translation from the danish language!

                  – Junior M
                  May 18 '09 at 23:08





                  sorry the bad translation from the danish language!

                  – Junior M
                  May 18 '09 at 23:08




                  7




                  7





                  Uh, a Time structure with a resolution of one minute? Give me a break...

                  – Lucero
                  Nov 20 '10 at 19:35





                  Uh, a Time structure with a resolution of one minute? Give me a break...

                  – Lucero
                  Nov 20 '10 at 19:35











                  6














                  A TimeSpan most certainly can store the time of the day - you just have to treat the value as the amount of time elapsed since midnight, basically the same way we read a clock.






                  share|improve this answer
























                  • TimeSpan does not store a time of day. Updated question to show you why.

                    – Ian Boyd
                    Dec 16 '08 at 21:18











                  • Just store it as UTC :)

                    – Greg Hurlman
                    Dec 17 '08 at 15:45
















                  6














                  A TimeSpan most certainly can store the time of the day - you just have to treat the value as the amount of time elapsed since midnight, basically the same way we read a clock.






                  share|improve this answer
























                  • TimeSpan does not store a time of day. Updated question to show you why.

                    – Ian Boyd
                    Dec 16 '08 at 21:18











                  • Just store it as UTC :)

                    – Greg Hurlman
                    Dec 17 '08 at 15:45














                  6












                  6








                  6







                  A TimeSpan most certainly can store the time of the day - you just have to treat the value as the amount of time elapsed since midnight, basically the same way we read a clock.






                  share|improve this answer













                  A TimeSpan most certainly can store the time of the day - you just have to treat the value as the amount of time elapsed since midnight, basically the same way we read a clock.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 16 '08 at 20:41









                  Greg HurlmanGreg Hurlman

                  15.5k64783




                  15.5k64783













                  • TimeSpan does not store a time of day. Updated question to show you why.

                    – Ian Boyd
                    Dec 16 '08 at 21:18











                  • Just store it as UTC :)

                    – Greg Hurlman
                    Dec 17 '08 at 15:45



















                  • TimeSpan does not store a time of day. Updated question to show you why.

                    – Ian Boyd
                    Dec 16 '08 at 21:18











                  • Just store it as UTC :)

                    – Greg Hurlman
                    Dec 17 '08 at 15:45

















                  TimeSpan does not store a time of day. Updated question to show you why.

                  – Ian Boyd
                  Dec 16 '08 at 21:18





                  TimeSpan does not store a time of day. Updated question to show you why.

                  – Ian Boyd
                  Dec 16 '08 at 21:18













                  Just store it as UTC :)

                  – Greg Hurlman
                  Dec 17 '08 at 15:45





                  Just store it as UTC :)

                  – Greg Hurlman
                  Dec 17 '08 at 15:45











                  6














                  Personally I'd create a custom Time struct that contains a DateTime instance, and which has similar properties, constructors etc. but doesn't expose days/months/etc. Just make all your public accessors pass through to the contained instance. Then you can simply have the epoch as a private static readonly DateTime field and it doesn't matter what value you choose as it's all neatly contained within your custom struct. In the rest of your code can simply write:



                  var time = new Time(16, 47, 58);





                  share|improve this answer




























                    6














                    Personally I'd create a custom Time struct that contains a DateTime instance, and which has similar properties, constructors etc. but doesn't expose days/months/etc. Just make all your public accessors pass through to the contained instance. Then you can simply have the epoch as a private static readonly DateTime field and it doesn't matter what value you choose as it's all neatly contained within your custom struct. In the rest of your code can simply write:



                    var time = new Time(16, 47, 58);





                    share|improve this answer


























                      6












                      6








                      6







                      Personally I'd create a custom Time struct that contains a DateTime instance, and which has similar properties, constructors etc. but doesn't expose days/months/etc. Just make all your public accessors pass through to the contained instance. Then you can simply have the epoch as a private static readonly DateTime field and it doesn't matter what value you choose as it's all neatly contained within your custom struct. In the rest of your code can simply write:



                      var time = new Time(16, 47, 58);





                      share|improve this answer













                      Personally I'd create a custom Time struct that contains a DateTime instance, and which has similar properties, constructors etc. but doesn't expose days/months/etc. Just make all your public accessors pass through to the contained instance. Then you can simply have the epoch as a private static readonly DateTime field and it doesn't matter what value you choose as it's all neatly contained within your custom struct. In the rest of your code can simply write:



                      var time = new Time(16, 47, 58);






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Dec 16 '08 at 21:53









                      Greg BeechGreg Beech

                      100k35185235




                      100k35185235























                          4














                          Given that DateTime.TimeOfDay returns a TimeSpan, I'd use that.



                          Why can't you use a TimeSpan? I don't understand your comment that it doesn't store times of day.






                          share|improve this answer




























                            4














                            Given that DateTime.TimeOfDay returns a TimeSpan, I'd use that.



                            Why can't you use a TimeSpan? I don't understand your comment that it doesn't store times of day.






                            share|improve this answer


























                              4












                              4








                              4







                              Given that DateTime.TimeOfDay returns a TimeSpan, I'd use that.



                              Why can't you use a TimeSpan? I don't understand your comment that it doesn't store times of day.






                              share|improve this answer













                              Given that DateTime.TimeOfDay returns a TimeSpan, I'd use that.



                              Why can't you use a TimeSpan? I don't understand your comment that it doesn't store times of day.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Dec 16 '08 at 20:40









                              JoeJoe

                              99.5k24152282




                              99.5k24152282























                                  3














                                  How about DateTime.Now.TimeOfDay, and use the TimeSpan?



                                  Re "because that doesn't store times of the day." - well, it does if you think of a TimeSpan as the time since midnight.



                                  A "duration", for example, screams TimeSpan.






                                  share|improve this answer
























                                  • Subtracting two DateTimes yields a TimeSpan.

                                    – Ian Boyd
                                    Dec 16 '08 at 21:19
















                                  3














                                  How about DateTime.Now.TimeOfDay, and use the TimeSpan?



                                  Re "because that doesn't store times of the day." - well, it does if you think of a TimeSpan as the time since midnight.



                                  A "duration", for example, screams TimeSpan.






                                  share|improve this answer
























                                  • Subtracting two DateTimes yields a TimeSpan.

                                    – Ian Boyd
                                    Dec 16 '08 at 21:19














                                  3












                                  3








                                  3







                                  How about DateTime.Now.TimeOfDay, and use the TimeSpan?



                                  Re "because that doesn't store times of the day." - well, it does if you think of a TimeSpan as the time since midnight.



                                  A "duration", for example, screams TimeSpan.






                                  share|improve this answer













                                  How about DateTime.Now.TimeOfDay, and use the TimeSpan?



                                  Re "because that doesn't store times of the day." - well, it does if you think of a TimeSpan as the time since midnight.



                                  A "duration", for example, screams TimeSpan.







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Dec 16 '08 at 20:39









                                  Marc GravellMarc Gravell

                                  779k19221282541




                                  779k19221282541













                                  • Subtracting two DateTimes yields a TimeSpan.

                                    – Ian Boyd
                                    Dec 16 '08 at 21:19



















                                  • Subtracting two DateTimes yields a TimeSpan.

                                    – Ian Boyd
                                    Dec 16 '08 at 21:19

















                                  Subtracting two DateTimes yields a TimeSpan.

                                  – Ian Boyd
                                  Dec 16 '08 at 21:19





                                  Subtracting two DateTimes yields a TimeSpan.

                                  – Ian Boyd
                                  Dec 16 '08 at 21:19











                                  3














                                  To display a TimeSpan formatted with local culture, simply add it to a date like DateTime.Today. Something like this:



                                  (DateTime.Today + timeSpan).ToString();



                                  Since your value really doesn't represent a date, you're better off storing it as a TimeSpan until the time comes to display it.






                                  share|improve this answer




























                                    3














                                    To display a TimeSpan formatted with local culture, simply add it to a date like DateTime.Today. Something like this:



                                    (DateTime.Today + timeSpan).ToString();



                                    Since your value really doesn't represent a date, you're better off storing it as a TimeSpan until the time comes to display it.






                                    share|improve this answer


























                                      3












                                      3








                                      3







                                      To display a TimeSpan formatted with local culture, simply add it to a date like DateTime.Today. Something like this:



                                      (DateTime.Today + timeSpan).ToString();



                                      Since your value really doesn't represent a date, you're better off storing it as a TimeSpan until the time comes to display it.






                                      share|improve this answer













                                      To display a TimeSpan formatted with local culture, simply add it to a date like DateTime.Today. Something like this:



                                      (DateTime.Today + timeSpan).ToString();



                                      Since your value really doesn't represent a date, you're better off storing it as a TimeSpan until the time comes to display it.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Dec 16 '08 at 21:32









                                      NeilNeil

                                      5,18543540




                                      5,18543540























                                          1














                                          I recommend DateTime.MinValue






                                          share|improve this answer




























                                            1














                                            I recommend DateTime.MinValue






                                            share|improve this answer


























                                              1












                                              1








                                              1







                                              I recommend DateTime.MinValue






                                              share|improve this answer













                                              I recommend DateTime.MinValue







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Dec 16 '08 at 20:41









                                              Joel CoehoornJoel Coehoorn

                                              306k95490721




                                              306k95490721























                                                  1














                                                  You can just create a new DateTime with a string literal.



                                                  String literal for time:



                                                  DateTime t = new DateTime("01:00:30");


                                                  String literal for date:



                                                  DateTime t = new DateTime("01/05/2008"); // english format
                                                  DateTime t = new DateTime("05.01.2008"); // german format


                                                  For a DateTime with date and time values:



                                                  DateTime t = new DateTime("01/05/2008T01:00:30");


                                                  In most cases, when creating a DateTime, i set it to DateTime.Now, if it is not actually set to anything else. If you instantiate an DateTime manually, you should beware of the DateTimeKind set correctly, otherwise this could lead to surprises.






                                                  share|improve this answer




























                                                    1














                                                    You can just create a new DateTime with a string literal.



                                                    String literal for time:



                                                    DateTime t = new DateTime("01:00:30");


                                                    String literal for date:



                                                    DateTime t = new DateTime("01/05/2008"); // english format
                                                    DateTime t = new DateTime("05.01.2008"); // german format


                                                    For a DateTime with date and time values:



                                                    DateTime t = new DateTime("01/05/2008T01:00:30");


                                                    In most cases, when creating a DateTime, i set it to DateTime.Now, if it is not actually set to anything else. If you instantiate an DateTime manually, you should beware of the DateTimeKind set correctly, otherwise this could lead to surprises.






                                                    share|improve this answer


























                                                      1












                                                      1








                                                      1







                                                      You can just create a new DateTime with a string literal.



                                                      String literal for time:



                                                      DateTime t = new DateTime("01:00:30");


                                                      String literal for date:



                                                      DateTime t = new DateTime("01/05/2008"); // english format
                                                      DateTime t = new DateTime("05.01.2008"); // german format


                                                      For a DateTime with date and time values:



                                                      DateTime t = new DateTime("01/05/2008T01:00:30");


                                                      In most cases, when creating a DateTime, i set it to DateTime.Now, if it is not actually set to anything else. If you instantiate an DateTime manually, you should beware of the DateTimeKind set correctly, otherwise this could lead to surprises.






                                                      share|improve this answer













                                                      You can just create a new DateTime with a string literal.



                                                      String literal for time:



                                                      DateTime t = new DateTime("01:00:30");


                                                      String literal for date:



                                                      DateTime t = new DateTime("01/05/2008"); // english format
                                                      DateTime t = new DateTime("05.01.2008"); // german format


                                                      For a DateTime with date and time values:



                                                      DateTime t = new DateTime("01/05/2008T01:00:30");


                                                      In most cases, when creating a DateTime, i set it to DateTime.Now, if it is not actually set to anything else. If you instantiate an DateTime manually, you should beware of the DateTimeKind set correctly, otherwise this could lead to surprises.







                                                      share|improve this answer












                                                      share|improve this answer



                                                      share|improve this answer










                                                      answered Dec 16 '08 at 21:47









                                                      Oliver FriedrichOliver Friedrich

                                                      6,22453545




                                                      6,22453545























                                                          1














                                                          May I suggest that in some cases a custom struct could do? It could have an Int32 backing value (there are 86 milion milliseconds in a day; this would fit in an Int32).



                                                          There could be get-only properties :



                                                          Hours
                                                          Minutes
                                                          Seconds
                                                          Milliseconds



                                                          You could also overload operators such as +, - and so on. Implement IEquatable, IComparable and whatever may be the case. Overload Equals, == . Overload and override ToString.



                                                          You could also provide more methods to construct from a DateTime or append to a datetime and so on.






                                                          share|improve this answer




























                                                            1














                                                            May I suggest that in some cases a custom struct could do? It could have an Int32 backing value (there are 86 milion milliseconds in a day; this would fit in an Int32).



                                                            There could be get-only properties :



                                                            Hours
                                                            Minutes
                                                            Seconds
                                                            Milliseconds



                                                            You could also overload operators such as +, - and so on. Implement IEquatable, IComparable and whatever may be the case. Overload Equals, == . Overload and override ToString.



                                                            You could also provide more methods to construct from a DateTime or append to a datetime and so on.






                                                            share|improve this answer


























                                                              1












                                                              1








                                                              1







                                                              May I suggest that in some cases a custom struct could do? It could have an Int32 backing value (there are 86 milion milliseconds in a day; this would fit in an Int32).



                                                              There could be get-only properties :



                                                              Hours
                                                              Minutes
                                                              Seconds
                                                              Milliseconds



                                                              You could also overload operators such as +, - and so on. Implement IEquatable, IComparable and whatever may be the case. Overload Equals, == . Overload and override ToString.



                                                              You could also provide more methods to construct from a DateTime or append to a datetime and so on.






                                                              share|improve this answer













                                                              May I suggest that in some cases a custom struct could do? It could have an Int32 backing value (there are 86 milion milliseconds in a day; this would fit in an Int32).



                                                              There could be get-only properties :



                                                              Hours
                                                              Minutes
                                                              Seconds
                                                              Milliseconds



                                                              You could also overload operators such as +, - and so on. Implement IEquatable, IComparable and whatever may be the case. Overload Equals, == . Overload and override ToString.



                                                              You could also provide more methods to construct from a DateTime or append to a datetime and so on.







                                                              share|improve this answer












                                                              share|improve this answer



                                                              share|improve this answer










                                                              answered Dec 17 '08 at 0:47









                                                              Andrei RîneaAndrei Rînea

                                                              12.5k12101152




                                                              12.5k12101152























                                                                  0














                                                                  Use a TimeSpan, and make it UTC if you have TimeZone issues.






                                                                  share|improve this answer




























                                                                    0














                                                                    Use a TimeSpan, and make it UTC if you have TimeZone issues.






                                                                    share|improve this answer


























                                                                      0












                                                                      0








                                                                      0







                                                                      Use a TimeSpan, and make it UTC if you have TimeZone issues.






                                                                      share|improve this answer













                                                                      Use a TimeSpan, and make it UTC if you have TimeZone issues.







                                                                      share|improve this answer












                                                                      share|improve this answer



                                                                      share|improve this answer










                                                                      answered Dec 16 '08 at 23:10









                                                                      TheSoftwareJediTheSoftwareJedi

                                                                      25.4k1991140




                                                                      25.4k1991140























                                                                          0














                                                                          No much difference compare to the accepted answer. Just to implement the idea.



                                                                          public class TimeOfDay
                                                                          {
                                                                          public DateTime time;
                                                                          public TimeOfDay(int Hour, int Minute, int Second)
                                                                          {
                                                                          time = DateTime.MinValue.Date.Add(new TimeSpan(Hour, Minute, Second));
                                                                          }
                                                                          }





                                                                          share|improve this answer




























                                                                            0














                                                                            No much difference compare to the accepted answer. Just to implement the idea.



                                                                            public class TimeOfDay
                                                                            {
                                                                            public DateTime time;
                                                                            public TimeOfDay(int Hour, int Minute, int Second)
                                                                            {
                                                                            time = DateTime.MinValue.Date.Add(new TimeSpan(Hour, Minute, Second));
                                                                            }
                                                                            }





                                                                            share|improve this answer


























                                                                              0












                                                                              0








                                                                              0







                                                                              No much difference compare to the accepted answer. Just to implement the idea.



                                                                              public class TimeOfDay
                                                                              {
                                                                              public DateTime time;
                                                                              public TimeOfDay(int Hour, int Minute, int Second)
                                                                              {
                                                                              time = DateTime.MinValue.Date.Add(new TimeSpan(Hour, Minute, Second));
                                                                              }
                                                                              }





                                                                              share|improve this answer













                                                                              No much difference compare to the accepted answer. Just to implement the idea.



                                                                              public class TimeOfDay
                                                                              {
                                                                              public DateTime time;
                                                                              public TimeOfDay(int Hour, int Minute, int Second)
                                                                              {
                                                                              time = DateTime.MinValue.Date.Add(new TimeSpan(Hour, Minute, Second));
                                                                              }
                                                                              }






                                                                              share|improve this answer












                                                                              share|improve this answer



                                                                              share|improve this answer










                                                                              answered Mar 31 '14 at 3:25









                                                                              Keep ThinkingKeep Thinking

                                                                              8216




                                                                              8216






























                                                                                  draft saved

                                                                                  draft discarded




















































                                                                                  Thanks for contributing an answer to Stack Overflow!


                                                                                  • Please be sure to answer the question. Provide details and share your research!

                                                                                  But avoid



                                                                                  • Asking for help, clarification, or responding to other answers.

                                                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                                                  To learn more, see our tips on writing great answers.




                                                                                  draft saved


                                                                                  draft discarded














                                                                                  StackExchange.ready(
                                                                                  function () {
                                                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f372601%2fc-sharp-datetime-what-date-to-use-when-im-using-just-the-time%23new-answer', 'question_page');
                                                                                  }
                                                                                  );

                                                                                  Post as a guest















                                                                                  Required, but never shown





















































                                                                                  Required, but never shown














                                                                                  Required, but never shown












                                                                                  Required, but never shown







                                                                                  Required, but never shown

































                                                                                  Required, but never shown














                                                                                  Required, but never shown












                                                                                  Required, but never shown







                                                                                  Required, but never shown







                                                                                  Popular posts from this blog

                                                                                  Full-time equivalent

                                                                                  Bicuculline

                                                                                  さくらももこ