C# TimeSpan
TimeSpan represents a period of time comparing with the class DateTime. So it can be measured as a number of date or time unit, like days, hours, minutes, seconds, etc no matter if the number is positive or negative.
Example 01-76-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
26
27
28
29
30
31
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
using System; class TestTimeSpanProperty { static void Main() { // Test Properties TimeSpan ts1 = new TimeSpan(1,2,3,4); // Initialized 1 day 100 hours 200 minutes and 300 seconds Console.WriteLine("ts1: {0}", ts1); Console.WriteLine("Days: {0}", ts1.Days); Console.WriteLine("Hours: {0}", ts1.Hours); Console.WriteLine("Minutes: {0}", ts1.Minutes); Console.WriteLine("Seconds: {0}", ts1.Seconds); Console.WriteLine("Total Days: {0}", ts1.TotalDays); Console.WriteLine("Total Hours: {0}", ts1.TotalHours); Console.WriteLine("Total Minutes: {0}", ts1.TotalMinutes); Console.WriteLine("Total Seconds: {0}", ts1.TotalSeconds); // Test Fields Ticks Console.WriteLine("Ticks Per Second: {0}", TimeSpan.TicksPerSecond); Console.WriteLine("Ticks Per Millisecond: {0}", TimeSpan.TicksPerMillisecond); // Test Field Zero TimeSpan ts2 = new TimeSpan(); Console.WriteLine("ts2: {0}", ts2); Console.WriteLine("ts1 is Zero: {0}", ts1.Equals(TimeSpan.Zero)); Console.WriteLine("ts2 is Zero: {0}", ts2.Equals(TimeSpan.Zero)); Console.Read(); } }
Output
ts1: 1.02:03:04 Days: 1 Hours: 2 Minutes: 3 Seconds: 4 Total Days: 1.08546296296296 Total Hours: 26.0511111111111 Total Minutes: 1563.06666666667 Total Seconds: 93784 Ticks Per Second: 10000000 Ticks Per Millisecond: 10000 ts2: 00:00:00 ts1 is Zero: False ts2 is Zero: True
Explanation
- Line 8-9: Create a TimeSpan object and output it.
- Line 10-13: Output days, hours, minutes and seconds separately. Either of them is a part of the TimeSpan.
- Line 14-17: Output total days, total hours, total minutes and total seconds separately. Either of them stands for the whole time but in different time units.
- Line 20-21: Tick is the minimum unit to stand for a period of time in TimeSpan. 1 millisecond = 10,000 ticks and 1 second = 10 million ticks.
- Line 24-25: Create a TimeSpan object by calling the default constructor then output it.
- Line 26-27: Apparently ts2 equals TimeSpan.Zero. TimeSpan.Zero is a read-only field which represents the zero TimeSpan value.
Example 01-76-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
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 TestTimeSpanMethods { static void Main() { // Test From Methods TimeSpan ts1 = TimeSpan.FromDays(2.5); // Assign 2.5 days to ts1 TimeSpan ts2 = TimeSpan.FromHours(12); // Assign 12 hours to ts2 Console.WriteLine("ts1: {0}", ts1); Console.WriteLine("ts2: {0}", ts2); // Add or Subtract Console.WriteLine("ts1 + ts2: {0}", ts1.Add(ts2)); Console.WriteLine("ts1 - ts2: {0}", ts1.Subtract(ts2)); Console.WriteLine("ts1 + ts2: {0}", ts1 + ts2); Console.WriteLine("ts1 - ts2: {0}", ts1 - ts2); // Work with DateTime DateTime dateOfBirth = new DateTime(1995,9,12); TimeSpan livedTime = DateTime.Now - dateOfBirth; Console.WriteLine("You lived {0} days.", Math.Truncate(livedTime.TotalDays)); Console.Read(); } }
Output
ts1: 2.12:00:00 ts2: 12:00:00 ts1 + ts2: 3.00:00:00 ts1 - ts2: 2.00:00:00 ts1 + ts2: 3.00:00:00 ts1 - ts2: 2.00:00:00 You lived 6987 days.
Explanation
- Line 8-11: Create 2 TimeSpan objects from static methods FromDays and FromHours and output the result. The other similar methods are FromMinutes, FromSeconds, FromMilliseconds and FromTicks. They all return a TimeSpan instance.
- Line 14-17: Addition or subtraction can be realized by either methods or operators.
- Line 21: The subtraction between 2 DateTime types returns a TimeSpan object.
- Line 22: Math.Truncate Returns an integer part of the TotalDays.