C#Datetime:当只使用“time”时,应该用什么日期?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (262)

使用C#中的日期时间来显示时间。具体是使用什么代码??

以下内容无效:

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

因为没有零月或者零日。

使用COM的零日期吗?

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

或者可能是SQLServer的?

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

意识到这是任意的,因为将忽略代码中的日期部分,但是能够使:

DateTime duration = time2 - time1;
提问于
用户回答回答于

就我个人而言,会创造一个Timestruct包含DateTime实例,并且具有类似的属性、构造函数等,但不公开天/月/等。只需将所有公共访问器传递到包含的实例即可。然后你就可以把这个时代当作private static readonly DateTime字段,而您选择的值并不重要,因为这些值都整齐地包含在的自定义结构中。在剩下的代码中,只需编写:

var time = new Time(16, 47, 58);
用户回答回答于

可以尝试下如下代码:

/// <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;
    }
}

扫码关注云+社区

领取腾讯云代金券