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

后端 未结 12 1565
陌清茗
陌清茗 2020-12-16 09:35

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 ther

相关标签:
12条回答
  • 2020-12-16 10:27

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

    0 讨论(0)
  • 2020-12-16 10:31

    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;
        }
    }
    
    0 讨论(0)
  • 2020-12-16 10:33

    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));
        }
    }
    
    0 讨论(0)
  • 2020-12-16 10:35

    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.

    0 讨论(0)
  • 2020-12-16 10:40

    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.

    0 讨论(0)
  • 2020-12-16 10:40

    I recommend DateTime.MinValue

    0 讨论(0)
提交回复
热议问题