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 there is no zero-th month or zero-th day:
// 4:37:58 PM DateTime time = new DateTime(0, 0, 0, 16, 47, 58);
Do I use COM's zero date?
// 4:37:58 PM DateTime time = new DateTime(1899, 12, 30, 16, 47, 58);
Or perhaps SQL Server's?
//4:37:58 PM DateTime time = new DateTime(1900, 1, 1, 16, 47, 58);
I realize it's arbitrary, since I'll be ignoring the date portions in code, but it would still be nice to be able to use:
DateTime duration = time2 - time1;
I think I like MinValue
DateTime time = DateTime.MinValue.Date.Add(new TimeSpan(16, 47, 58));
Note: I can't use a
TimeSpan, because that doesn't store times of the day. And the reason I know that is because there's no way to display its contents as a time.
Which is to say that
TimeSpan records a span of time, not a time of day, e.g.:
TimeSpan t = new TimeSpan(16, 47, 58); t.ToString();
returns a span of time in the format hours:minutes:seconds, e.g.:
rather than a time:
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
In other words, there is a difference between a timespan, and a time. And also realize that
TimeSpan doesn't provide a mechanism to convert a span of time into a time of day - and there is a reason for that.