31

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

例如,以下内容无效,因为没有第零个月或第零天:

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

注意:我不能使用 a TimeSpan,因为它不存储一天中的时间。我知道的原因是因为无法将其内容显示为时间。

也就是说,TimeSpan记录的是时间跨度,而不是一天中的时间,例如:

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

返回格式为hours : minutes : seconds的时间跨度,例如:

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并没有提供将时间跨度转换为一天中的时间的机制——这是有原因的。

4

12 回答 12

26

DateTime.MinValue 呢?

于 2008-12-16T20:40:35.800 回答
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;
    }
}
于 2009-05-18T23:07:49.260 回答
7

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

于 2008-12-16T20:41:08.280 回答
6

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

var time = new Time(16, 47, 58);
于 2008-12-16T21:53:23.623 回答
4

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

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

于 2008-12-16T20:40:51.150 回答
3

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

重新“因为它不存储一天中的时间。” - 好吧,如果您将 aTimeSpan视为自午夜以来的时间,它确实如此。

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

于 2008-12-16T20:39:10.497 回答
3

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

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

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

于 2008-12-16T21:32:17.987 回答
1

我建议DateTime.MinValue

于 2008-12-16T20:41:26.623 回答
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,否则可能会导致意外。

于 2008-12-16T21:47:06.070 回答
1

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

可能有 get-only 属性:

时分秒毫秒

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

您还可以提供更多方法来从 DateTime 构造或附加到 datetime 等等。

于 2008-12-17T00:47:28.827 回答
0

如果您有 TimeZone 问题,请使用 TimeSpan,并将其设为 UTC。

于 2008-12-16T23:10:34.793 回答
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));
    }
}
于 2014-03-31T03:25:29.000 回答