Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# DateTime: What "date" to use when I'm using just the "time"?

Tags:

c#

datetime

time

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; 

Answer

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.:

16:47:58 

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.

like image 232
Ian Boyd Avatar asked Dec 16 '08 20:12

Ian Boyd


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


1 Answers

what about DateTime.MinValue?

like image 142
Joachim Kerschbaumer Avatar answered Oct 08 '22 23:10

Joachim Kerschbaumer