C# 如何在 .NET 中表示仅时间值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2037283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I represent a time only value in .NET?
提问by sduplooy
Is there a way one can represent a time only value in .NET without the date? For example, indicating the opening time of a shop?
有没有一种方法可以在没有日期的情况下表示 .NET 中的仅时间值?例如,指示商店的营业时间?
TimeSpan
indicates a range, whereas I only want to store a time value. Using DateTime
to indicate this would result in new DateTime(1,1,1,8,30,0)
which is not really desirable.
TimeSpan
表示一个范围,而我只想存储一个时间值。使用DateTime
来表示这将导致新DateTime(1,1,1,8,30,0)
的不是真正可取的。
采纳答案by Jon Skeet
As others have said, you canuse a DateTime
and ignore the date, or use a TimeSpan
. Personally I'm not keen on either of these solutions, as neither type really reflects the concept you're trying to represent - I regard the date/time types in .NET as somewhat on the sparse side which is one of the reasons I started Noda Time. In Noda Time, you can use the LocalTime
type to represent a time of day.
正如其他人所说,您可以使用 aDateTime
并忽略日期,或使用TimeSpan
. 就我个人而言,我并不热衷于这些解决方案中的任何一个,因为这两种类型都没有真正反映您试图表示的概念 - 我认为 .NET 中的日期/时间类型在某种程度上是稀疏的,这是我开始的原因之一野田时间。在 Noda Time 中,您可以使用LocalTime
类型来表示一天中的时间。
One thing to consider: the time of day is not necessarily the length of time since midnight on the same day...
需要考虑的一件事:一天中的时间不一定是从同一天午夜开始的时间长度......
(As another aside, if you're also wanting to represent a closingtime of a shop, you may find that you want to represent 24:00, i.e. the time at the end of the day. Most date/time APIs - including Noda Time - don't allow that to be represented as a time-of-day value.)
(另外,如果您还想表示商店的关闭时间,您可能会发现您想要表示 24:00,即一天结束的时间。大多数日期/时间 API - 包括 Noda时间 - 不允许将其表示为一天中的时间值。)
回答by John G
You can use timespan
您可以使用时间跨度
TimeSpan timeSpan = new TimeSpan(2, 14, 18);
Console.WriteLine(timeSpan.ToString()); // Displays "02:14:18".
[Edit]
Considering the other answers and the edit to the question, I would still use TimeSpan. No point in creating a new structure where an existing one from the framework suffice.
On these lines you would end up duplicating many native data types.
[编辑]
考虑到其他答案和对问题的编辑,我仍然会使用 TimeSpan。没有必要创建一个新结构,其中框架中的现有结构就足够了。
在这些行中,您最终会复制许多本机数据类型。
回答by Rubens Farias
If that empty Date
really bugs you, you can also to create a simpler Time
structure:
如果那个空Date
真的让你感到困扰,你也可以创建一个更简单的Time
结构:
// more work is required to make this even close to production ready
class Time
{
// TODO: don't forget to add validation
public int Hours { get; set; }
public int Minutes { get; set; }
public int Seconds { get; set; }
public override string ToString()
{
return String.Format(
"{0:00}:{1:00}:{2:00}",
this.Hours, this.Minutes, this.Seconds);
}
}
Or, why to bother: if you don't need to do any calculation with that information, just store it as String
.
或者,为什么要麻烦:如果您不需要对该信息进行任何计算,只需将其存储为String
.
回答by Jason Williams
If you don't want to use a DateTime or TimeSpan, and just want to store the time of day, you could just store the seconds since midnight in an Int32, or (if you don't even want seconds) the minutes since midnight would fit into an Int16. It would be trivial to write the few methods required to access the Hour, Minute and Second from such a value.
如果您不想使用 DateTime 或 TimeSpan,而只想存储一天中的时间,则可以将自午夜以来的秒数存储在 Int32 中,或者(如果您甚至不想要秒数)自午夜以来的分钟数将适合 Int16。编写从这样的值访问小时、分钟和秒所需的几个方法将是微不足道的。
The only reason I can think of to avoid DateTime/TimeSpan would be if the size of the structure is critical.
我能想到的避免 DateTime/TimeSpan 的唯一原因是结构的大小是否至关重要。
(Of course, if you use a simple scheme like the above wrapped in a class, then it would also be trivial to replace the storage with a TimeSpan in future if you suddenly realise that would give you an advantage)
(当然,如果你使用像上面这样的简单方案包裹在一个类中,那么如果你突然意识到这会给你带来好处,那么将来用 TimeSpan 替换存储也是微不足道的)
回答by bugfixr
I say use a DateTime. If you don't need the date portion, just ignore it. If you need to display just the time to the user, output it formatted to the user like this:
我说使用日期时间。如果您不需要日期部分,请忽略它。如果您只需要向用户显示时间,请将其格式化输出给用户,如下所示:
DateTime.Now.ToString("t"); // outputs 10:00 PM
It seems like all the extra work of making a new class or even using a TimeSpan is unnecessary.
创建新类甚至使用 TimeSpan 的所有额外工作似乎都是不必要的。
回答by Chibueze Opata
I think Rubens' class is a good idea so thought to make an immutable sample of his Time class with basic validation.
我认为 Rubens 的课程是一个好主意,因此我想通过基本验证制作他的 Time 课程的不可变样本。
class Time
{
public int Hours { get; private set; }
public int Minutes { get; private set; }
public int Seconds { get; private set; }
public Time(uint h, uint m, uint s)
{
if(h > 23 || m > 59 || s > 59)
{
throw new ArgumentException("Invalid time specified");
}
Hours = (int)h; Minutes = (int)m; Seconds = (int)s;
}
public Time(DateTime dt)
{
Hours = dt.Hour;
Minutes = dt.Minute;
Seconds = dt.Second;
}
public override string ToString()
{
return String.Format(
"{0:00}:{1:00}:{2:00}",
this.Hours, this.Minutes, this.Seconds);
}
}
回答by Jules
In addition to Chibueze Opata:
除了Chibueze Opata:
class Time
{
public int Hours { get; private set; }
public int Minutes { get; private set; }
public int Seconds { get; private set; }
public Time(uint h, uint m, uint s)
{
if(h > 23 || m > 59 || s > 59)
{
throw new ArgumentException("Invalid time specified");
}
Hours = (int)h; Minutes = (int)m; Seconds = (int)s;
}
public Time(DateTime dt)
{
Hours = dt.Hour;
Minutes = dt.Minute;
Seconds = dt.Second;
}
public override string ToString()
{
return String.Format(
"{0:00}:{1:00}:{2:00}",
this.Hours, this.Minutes, this.Seconds);
}
public void AddHours(uint h)
{
this.Hours += (int)h;
}
public void AddMinutes(uint m)
{
this.Minutes += (int)m;
while(this.Minutes > 59)
this.Minutes -= 60;
this.AddHours(1);
}
public void AddSeconds(uint s)
{
this.Seconds += (int)s;
while(this.Seconds > 59)
this.Seconds -= 60;
this.AddMinutes(1);
}
}
回答by Steve Lautenschlager
Here's a full featured TimeOfDay class.
这是一个功能齐全的 TimeOfDay 类。
This is overkill for simple cases, but if you need more advanced functionality like I did, this may help.
这对于简单的情况来说太过分了,但是如果您像我一样需要更高级的功能,这可能会有所帮助。
It can handle the corner cases, some basic math, comparisons, interaction with DateTime, parsing, etc.
它可以处理极端情况、一些基本数学、比较、与 DateTime 的交互、解析等。
Below is the source code for the TimeOfDay class. You can see usage examples and learn more here:
下面是 TimeOfDay 类的源代码。您可以在此处查看使用示例并了解更多信息:
This class uses DateTime for most of its internal calculations and comparisons so that we can leverage all of the knowledge already embedded in DateTime.
此类使用 DateTime 进行大部分内部计算和比较,以便我们可以利用已嵌入 DateTime 中的所有知识。
// Author: Steve Lautenschlager, CambiaResearch.com
// License: MIT
using System;
using System.Text.RegularExpressions;
namespace Cambia
{
public class TimeOfDay
{
private const int MINUTES_PER_DAY = 60 * 24;
private const int SECONDS_PER_DAY = SECONDS_PER_HOUR * 24;
private const int SECONDS_PER_HOUR = 3600;
private static Regex _TodRegex = new Regex(@"\d?\d:\d\d:\d\d|\d?\d:\d\d");
public TimeOfDay()
{
Init(0, 0, 0);
}
public TimeOfDay(int hour, int minute, int second = 0)
{
Init(hour, minute, second);
}
public TimeOfDay(int hhmmss)
{
Init(hhmmss);
}
public TimeOfDay(DateTime dt)
{
Init(dt);
}
public TimeOfDay(TimeOfDay td)
{
Init(td.Hour, td.Minute, td.Second);
}
public int HHMMSS
{
get
{
return Hour * 10000 + Minute * 100 + Second;
}
}
public int Hour { get; private set; }
public int Minute { get; private set; }
public int Second { get; private set; }
public double TotalDays
{
get
{
return TotalSeconds / (24d * SECONDS_PER_HOUR);
}
}
public double TotalHours
{
get
{
return TotalSeconds / (1d * SECONDS_PER_HOUR);
}
}
public double TotalMinutes
{
get
{
return TotalSeconds / 60d;
}
}
public int TotalSeconds
{
get
{
return Hour * 3600 + Minute * 60 + Second;
}
}
public bool Equals(TimeOfDay other)
{
if (other == null) { return false; }
return TotalSeconds == other.TotalSeconds;
}
public override bool Equals(object obj)
{
if (obj == null) { return false; }
TimeOfDay td = obj as TimeOfDay;
if (td == null) { return false; }
else { return Equals(td); }
}
public override int GetHashCode()
{
return TotalSeconds;
}
public DateTime ToDateTime(DateTime dt)
{
return new DateTime(dt.Year, dt.Month, dt.Day, Hour, Minute, Second);
}
public override string ToString()
{
return ToString("HH:mm:ss");
}
public string ToString(string format)
{
DateTime now = DateTime.Now;
DateTime dt = new DateTime(now.Year, now.Month, now.Day, Hour, Minute, Second);
return dt.ToString(format);
}
public TimeSpan ToTimeSpan()
{
return new TimeSpan(Hour, Minute, Second);
}
public DateTime ToToday()
{
var now = DateTime.Now;
return new DateTime(now.Year, now.Month, now.Day, Hour, Minute, Second);
}
#region -- Static --
public static TimeOfDay Midnight { get { return new TimeOfDay(0, 0, 0); } }
public static TimeOfDay Noon { get { return new TimeOfDay(12, 0, 0); } }
public static TimeOfDay operator -(TimeOfDay t1, TimeOfDay t2)
{
DateTime now = DateTime.Now;
DateTime dt1 = new DateTime(now.Year, now.Month, now.Day, t1.Hour, t1.Minute, t1.Second);
TimeSpan ts = new TimeSpan(t2.Hour, t2.Minute, t2.Second);
DateTime dt2 = dt1 - ts;
return new TimeOfDay(dt2);
}
public static bool operator !=(TimeOfDay t1, TimeOfDay t2)
{
if (ReferenceEquals(t1, t2)) { return true; }
else if (ReferenceEquals(t1, null)) { return true; }
else
{
return t1.TotalSeconds != t2.TotalSeconds;
}
}
public static bool operator !=(TimeOfDay t1, DateTime dt2)
{
if (ReferenceEquals(t1, null)) { return false; }
DateTime dt1 = new DateTime(dt2.Year, dt2.Month, dt2.Day, t1.Hour, t1.Minute, t1.Second);
return dt1 != dt2;
}
public static bool operator !=(DateTime dt1, TimeOfDay t2)
{
if (ReferenceEquals(t2, null)) { return false; }
DateTime dt2 = new DateTime(dt1.Year, dt1.Month, dt1.Day, t2.Hour, t2.Minute, t2.Second);
return dt1 != dt2;
}
public static TimeOfDay operator +(TimeOfDay t1, TimeOfDay t2)
{
DateTime now = DateTime.Now;
DateTime dt1 = new DateTime(now.Year, now.Month, now.Day, t1.Hour, t1.Minute, t1.Second);
TimeSpan ts = new TimeSpan(t2.Hour, t2.Minute, t2.Second);
DateTime dt2 = dt1 + ts;
return new TimeOfDay(dt2);
}
public static bool operator <(TimeOfDay t1, TimeOfDay t2)
{
if (ReferenceEquals(t1, t2)) { return true; }
else if (ReferenceEquals(t1, null)) { return true; }
else
{
return t1.TotalSeconds < t2.TotalSeconds;
}
}
public static bool operator <(TimeOfDay t1, DateTime dt2)
{
if (ReferenceEquals(t1, null)) { return false; }
DateTime dt1 = new DateTime(dt2.Year, dt2.Month, dt2.Day, t1.Hour, t1.Minute, t1.Second);
return dt1 < dt2;
}
public static bool operator <(DateTime dt1, TimeOfDay t2)
{
if (ReferenceEquals(t2, null)) { return false; }
DateTime dt2 = new DateTime(dt1.Year, dt1.Month, dt1.Day, t2.Hour, t2.Minute, t2.Second);
return dt1 < dt2;
}
public static bool operator <=(TimeOfDay t1, TimeOfDay t2)
{
if (ReferenceEquals(t1, t2)) { return true; }
else if (ReferenceEquals(t1, null)) { return true; }
else
{
if (t1 == t2) { return true; }
return t1.TotalSeconds <= t2.TotalSeconds;
}
}
public static bool operator <=(TimeOfDay t1, DateTime dt2)
{
if (ReferenceEquals(t1, null)) { return false; }
DateTime dt1 = new DateTime(dt2.Year, dt2.Month, dt2.Day, t1.Hour, t1.Minute, t1.Second);
return dt1 <= dt2;
}
public static bool operator <=(DateTime dt1, TimeOfDay t2)
{
if (ReferenceEquals(t2, null)) { return false; }
DateTime dt2 = new DateTime(dt1.Year, dt1.Month, dt1.Day, t2.Hour, t2.Minute, t2.Second);
return dt1 <= dt2;
}
public static bool operator ==(TimeOfDay t1, TimeOfDay t2)
{
if (ReferenceEquals(t1, t2)) { return true; }
else if (ReferenceEquals(t1, null)) { return true; }
else { return t1.Equals(t2); }
}
public static bool operator ==(TimeOfDay t1, DateTime dt2)
{
if (ReferenceEquals(t1, null)) { return false; }
DateTime dt1 = new DateTime(dt2.Year, dt2.Month, dt2.Day, t1.Hour, t1.Minute, t1.Second);
return dt1 == dt2;
}
public static bool operator ==(DateTime dt1, TimeOfDay t2)
{
if (ReferenceEquals(t2, null)) { return false; }
DateTime dt2 = new DateTime(dt1.Year, dt1.Month, dt1.Day, t2.Hour, t2.Minute, t2.Second);
return dt1 == dt2;
}
public static bool operator >(TimeOfDay t1, TimeOfDay t2)
{
if (ReferenceEquals(t1, t2)) { return true; }
else if (ReferenceEquals(t1, null)) { return true; }
else
{
return t1.TotalSeconds > t2.TotalSeconds;
}
}
public static bool operator >(TimeOfDay t1, DateTime dt2)
{
if (ReferenceEquals(t1, null)) { return false; }
DateTime dt1 = new DateTime(dt2.Year, dt2.Month, dt2.Day, t1.Hour, t1.Minute, t1.Second);
return dt1 > dt2;
}
public static bool operator >(DateTime dt1, TimeOfDay t2)
{
if (ReferenceEquals(t2, null)) { return false; }
DateTime dt2 = new DateTime(dt1.Year, dt1.Month, dt1.Day, t2.Hour, t2.Minute, t2.Second);
return dt1 > dt2;
}
public static bool operator >=(TimeOfDay t1, TimeOfDay t2)
{
if (ReferenceEquals(t1, t2)) { return true; }
else if (ReferenceEquals(t1, null)) { return true; }
else
{
return t1.TotalSeconds >= t2.TotalSeconds;
}
}
public static bool operator >=(TimeOfDay t1, DateTime dt2)
{
if (ReferenceEquals(t1, null)) { return false; }
DateTime dt1 = new DateTime(dt2.Year, dt2.Month, dt2.Day, t1.Hour, t1.Minute, t1.Second);
return dt1 >= dt2;
}
public static bool operator >=(DateTime dt1, TimeOfDay t2)
{
if (ReferenceEquals(t2, null)) { return false; }
DateTime dt2 = new DateTime(dt1.Year, dt1.Month, dt1.Day, t2.Hour, t2.Minute, t2.Second);
return dt1 >= dt2;
}
/// <summary>
/// Input examples:
/// 14:21:17 (2pm 21min 17sec)
/// 02:15 (2am 15min 0sec)
/// 2:15 (2am 15min 0sec)
/// 2/1/2017 14:21 (2pm 21min 0sec)
/// TimeOfDay=15:13:12 (3pm 13min 12sec)
/// </summary>
public static TimeOfDay Parse(string s)
{
// We will parse any section of the text that matches this
// pattern: dd:dd or dd:dd:dd where the first doublet can
// be one or two digits for the hour. But minute and second
// must be two digits.
Match m = _TodRegex.Match(s);
string text = m.Value;
string[] fields = text.Split(':');
if (fields.Length < 2) { throw new ArgumentException("No valid time of day pattern found in input text"); }
int hour = Convert.ToInt32(fields[0]);
int min = Convert.ToInt32(fields[1]);
int sec = fields.Length > 2 ? Convert.ToInt32(fields[2]) : 0;
return new TimeOfDay(hour, min, sec);
}
#endregion
private void Init(int hour, int minute, int second)
{
if (hour < 0 || hour > 23) { throw new ArgumentException("Invalid hour, must be from 0 to 23."); }
if (minute < 0 || minute > 59) { throw new ArgumentException("Invalid minute, must be from 0 to 59."); }
if (second < 0 || second > 59) { throw new ArgumentException("Invalid second, must be from 0 to 59."); }
Hour = hour;
Minute = minute;
Second = second;
}
private void Init(int hhmmss)
{
int hour = hhmmss / 10000;
int min = (hhmmss - hour * 10000) / 100;
int sec = (hhmmss - hour * 10000 - min * 100);
Init(hour, min, sec);
}
private void Init(DateTime dt)
{
Init(dt.Hour, dt.Minute, dt.Second);
}
}
}