C#DateTime:当我只使用“时间”时使用什么“日期”?

时间:2008-12-16 20:35:56

标签: c# datetime time

我在C#中使用DateTime来显示时间。在构建时间时,每个人都使用什么日期部分?

E.g。以下内容无效,因为没有零日或零日:

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

或者也许是SQL Server的?

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

我意识到这是随意的,因为我会忽略代码中的日期部分,但是能够使用它仍然很好:

DateTime duration = time2 - time1;

答案

我想我喜欢 MinValue

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

注意:我无法使用TimeSpan,因为它不会存储当天的时间。我知道的原因是因为没有办法将其内容显示为时间。

也就是说TimeSpan记录时间范围,而非时间,例如:

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

小时格式返回一段时间:分钟,例如:

16:47:58

而不是时间:

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

换句话说,时间跨度和时间之间存在差异。并且还意识到TimeSpan没有提供将时间跨度转换为一天中的时间的机制 - 这是有原因的。

12 个答案:

答案 0 :(得分:25)

DateTime.MinValue怎么样?

答案 1 :(得分:8)

让我们帮助那些想要时间结构的人:

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

答案 2 :(得分:6)

TimeSpan肯定可以存储一天中的时间 - 您只需将该值视为自午夜起所经过的时间量,基本上与我们读取时钟的方式相同。

答案 3 :(得分:6)

就个人而言,我会创建一个自定义Time struct,其中包含DateTime个实例,并且具有类似的属性,构造函数等,但不会暴露天/月/等。只需将所有公共访问者传递给包含的实例即可。然后你可以简单地将时期作为private static readonly DateTime字段,并且你选择什么值并不重要,因为它全部包含在你的自定义结构中。在其余的代码中可以简单地写:

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

答案 4 :(得分:4)

鉴于DateTime.TimeOfDay返回TimeSpan,我会使用它。

为什么不能使用TimeSpan?我不明白你的评论,它没有存储时间。

答案 5 :(得分:3)

DateTime.Now.TimeOfDay怎么样,并使用TimeSpan

重新“因为那不存储当天的时间。” - 好吧,如果你认为TimeSpan是午夜以来的时间,那就是这样。

“持续时间”,例如,尖叫TimeSpan

答案 6 :(得分:3)

要显示使用本地文化格式化的TimeSpan,只需将其添加到DateTime.Today等日期即可。像这样:

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

由于您的值实际上并不代表日期,因此最好将其存储为TimeSpan,直到显示它为止。

答案 7 :(得分:1)

我建议DateTime.MinValue

答案 8 :(得分:1)

您可以使用字符串文字创建一个新的DateTime。

时间字符串文字:

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

日期的字符串文字:

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

对于具有日期和时间值的DateTime:

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

在大多数情况下,在创建DateTime时,我将其设置为DateTime.Now,如果它实际上没有设置为其他任何内容。如果手动实例化DateTime,则应该注意正确设置DateTimeKind,否则可能会导致意外。

答案 9 :(得分:1)

我可以建议在某些情况下自定义结构可以吗?它可以有一个Int32支持值(一天有86毫秒毫秒;这适合Int32)。

可能只有get属性:

小时 分钟 秒 毫秒

您还可以重载+, - 等运算符。实现IEquatable,IComparable以及可能的情况。重载等于,==。重载并覆盖ToString。

您还可以提供更多方法来构建DateTime或附加到日期时间等等。

答案 10 :(得分:0)

使用TimeSpan,如果遇到TimeZone问题,请将其设为UTC。

答案 11 :(得分:0)

与接受的答案相比没有太大区别。只是为了实现这个想法。

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