C# DateTime
DateTime is not a built-in data type but an important data type to stand for a specific point of time. DateTime is a struct of the namespace System. For details, you can check it here.
In C#, there is no predefined Date class but the Date can be got from the property Date of the DateTime. The following example will output the most properties of the DateTime class.
Example 01-75-01
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using System; class TestDateTimeProperty { static void Main() { DateTime t1 = new DateTime(2001,10,20,10,20,30); Console.WriteLine("DateTime: {0}", t1); Console.WriteLine("Date: {0}", t1.Date); DateTime t2 = DateTime.Now; Console.WriteLine("Year: {0}", t2.Year); Console.WriteLine("Month: {0}", t2.Month); Console.WriteLine("Day: {0}", t2.Day); Console.WriteLine("Hour: {0}", t2.Hour); Console.WriteLine("Minute: {0}", t2.Minute); Console.WriteLine("Second: {0}", t2.Second); Console.WriteLine("Millisecond: {0}", t2.Millisecond); Console.WriteLine("Day of Week: {0}", t2.DayOfWeek); Console.WriteLine("Day of Year: {0}", t2.DayOfYear); Console.WriteLine("Time of Day: {0}", t2.TimeOfDay); Console.Read(); } }
Output
DateTime: 10/20/2001 10:20:30 AM Date: 10/20/2001 12:00:00 AM Year: 2014 Month: 10 Day: 24 Hour: 9 Minute: 45 Second: 1 Millisecond: 53 Day of Week: Friday Day of Year: 297 Time of Day: 09:45:01.0533188
Explanation
- Line 7-8: Create a datetime object and output the date and time. The constructor is called with 6 parameters.
- Line 9: Get the date part of the datetime and t1.Date is also returned a DateTime type but the time parts are all zero.
- Line 11: Get the current date time. If you want current date only, you can use DateTime.Today. Both Now and DateTime are static properties.
- Line 12-21: Output the non-static properties of the object t2.
Example 01-75-02
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System; class TestDateTimeMethod { static void Main() { // AddDays method DateTime today = DateTime.Now; DateTime yesterday = today.AddDays(-1); DateTime tomorrow = today.AddDays(1); Console.WriteLine("Yesterday: {0}", yesterday.ToString("yyyy-mm-dd")); Console.WriteLine("Tomorrow: {0}", tomorrow.ToString("d")); Console.WriteLine("Tomorrow: {0}", tomorrow.ToShortDateString()); // Datetime Compare int result = DateTime.Compare(yesterday, tomorrow); if (result > 0) { Console.WriteLine("{0} is later than {1}", yesterday, tomorrow); } else { if (result < 0) { Console.WriteLine("{0} is earlier than {1}", yesterday, tomorrow); } else { Console.WriteLine("{0} is equal to {1}", yesterday, tomorrow); } } // Detetime Operator if (yesterday >= tomorrow) { Console.WriteLine("Yesterday >= Tommrrow"); } else { Console.WriteLine("Yesterday < Tommrrow"); } Console.Read(); } }
Output
Yesterday: 2014-31-23 Tomorrow: 10/25/2014 Tomorrow: 10/25/2014 10/23/2014 10:31:17 AM is earlier than 10/25/2014 10:31:17 AM Yesterday < Tommrrow
Explanation
- Line 8-10: AddDays(num) returns a DateTime object which subtract num days of the current object.
- Line 11-13: Output in different formats. The codes in line 12 and 13 are the same.
- Line 16-31: Compare 2 DateTime objects and returns a value to indicate if they are equal or one object is earlier or later than the other one.
- Line 34-41: We also can simply use the operator >, >=, <, <=, == or != to compare 2 DateTime objects.