Get properties of TimeSpan
using System;
public class Example
{
public static void Main()
{
DateTime date1 = new DateTime(2009, 1, 1, 8, 0, 15);
DateTime date2 = new DateTime(2010, 8, 11, 13, 30, 30);
TimeSpan interval = date2 - date1;
Console.WriteLine("{0} - {1} = {2}", date2, date1, interval.ToString());
Console.WriteLine(" {0,-35} {1,20}", "Value of Days Component:", interval.Days);
Console.WriteLine(" {0,-35} {1,20}", "Total Number of Days:", interval.TotalDays);
Console.WriteLine(" {0,-35} {1,20}", "Value of Hours Component:", interval.Hours);
Console.WriteLine(" {0,-35} {1,20}", "Total Number of Hours:", interval.TotalHours);
Console.WriteLine(" {0,-35} {1,20}", "Value of Minutes Component:", interval.Minutes);
Console.WriteLine(" {0,-35} {1,20}", "Total Number of Minutes:", interval.TotalMinutes);
Console.WriteLine(" {0,-35} {1,20:N0}", "Value of Seconds Component:", interval.Seconds);
Console.WriteLine(" {0,-35} {1,20:N0}", "Total Number of Seconds:", interval.TotalSeconds);
Console.WriteLine(" {0,-35} {1,20:N0}", "Value of Milliseconds Component:", interval.Milliseconds);
Console.WriteLine(" {0,-35} {1,20:N0}", "Total Number of Milliseconds:", interval.TotalMilliseconds);
Console.WriteLine(" {0,-35} {1,20:N0}", "Ticks:", interval.Ticks);
}
}
Related examples in the same category
1. | new TimeSpan(2, 12, 0, 0) | | |
2. | TimeSpan.TicksPerDay | | |
3. | Initialize a time span to zero | | |
4. | Initialize a time span to 14 days | | |
5. | Initialize a time span to 1:02:03 | | |
6. | Initialize a time span to 250 milliseconds | | |
7. | Initalize a time span to 99 days, 23 hours, 59 minutes, and 59.9999999 seconds | | |
8. | Calculation based on the TimeSpan | | |
9. | Subtract 15 minutes from the current TimeSpan and print the result | | |
10. | Measuring the Time Taken to Add Some Numbers | | |
11. | Use FromDays(), FromHours(), FromMinutes(), FromSeconds(), FromMilliseconds(), and FromTicks() methods to create new TimeSpan instances | | |
12. | Use the Parse() method to convert strings to TimeSpan instances | | |
13. | Use the Add() method to add a TimeSpan instance to another | | |
14. | Use the Subtract() method to subtract a TimeSpan instance from another | | |
15. | Use the Duration() method to add two TimeSpan instances | | |
16. | Use the Negate() method to add two TimeSpan instances | | |
17. | Create a TimeSpan instance, specifying the hours, minutes, and seconds | | |
18. | Create a TimeSpan instance, specifying the days, hours, minutes, and seconds | | |
19. | Create a TimeSpan instance, specifying the days, hours, minutes, seconds, and milliseconds | | |
20. | Create a TimeSpan instance, specifying the number of ticks | | |
21. | Display the properties for myTimeSpan | | |
22. | Initalize a timespan to 25 milliseconds | | |
23. | Custom TimeSpan Format Strings | | |
24. | Create TimeSpan value from seconds | | |
25. | TimeSpan calculations | | |
26. | Create a new TimeSpan to a specified number of hours, minutes, and seconds. | | |
27. | Create a new TimeSpan to a specified number of days, hours, minutes, and seconds. | | |
28. | Create a TimeSpan to a specified number of days, hours, minutes, seconds, and milliseconds. | | |
29. | Create a new TimeSpan to the specified number of ticks. | | |
30. | Zero TimeSpan | | |
31. | Default output of ToString() method | | |
32. | Subtract one DateTime from another you get a TimeSpan | | |
33. | Elapsed Time Today with TimeSpan | | |
34. | Returns a new TimeSpan object whose value is the absolute value of the current TimeSpan object. | | |
35. | Example of the TimeSpan relational operators. | | |
36. | Get a TimeSpan that represents a specified number of days | | |
37. | Create a TimeSpan that represents a specified number of hours | | |
38. | Returns a TimeSpan that represents a specified number of milliseconds. | | |
39. | Returns a TimeSpan that represents a specified number of minutes | | |
40. | Create a TimeSpan that represents a specified number of seconds | | |
41. | Create a TimeSpan from units of ticks. | | |
42. | Predefined TimeSpan value:MaxValue, MinValue, Zero, TicksPerDay, TicksPersHour | | |
43. | Converts string to TimeSpan | | |
44. | Converts string to TimeSpan by using the specified format and culture-specific format information. | | |
45. | Convert TimeSpan in whole and fractional days | | |
46. | Gets the value of TimeSpan in whole and fractional hours. | | |
47. | Gets the value of TimeSpan in whole and fractional milliseconds. | | |
48. | Gets the value of TimeSpan in whole and fractional minutes. | | |
49. | Gets the value of TimeSpan in whole and fractional seconds. | | |
50. | Duration data structure | | |