TimeSpan in C#
The struct TimeSpan in C# represents a time interval that is
difference between two times measured in number of days, hours, minutes, and
seconds. The TimeSpan is used to compare two C# DateTime objects to find the
difference between two dates.
Creating TimeSpan
TimeSpan struct has
the following overloaded forms:
The following code snippet crates a
TimeSpan from days, hours, and minutes.
The common method of creating a TimeSpan is finding the
difference between two DateTime objects, The following code snippet is example
of getting the interval between two DateTime objects.
TimeSpan Properties
C# TimeSpan class
properties are Days, Hours, Minutes, Seconds, Milliseconds, and Ticks that
returns days, hours, minutes, seconds, and milliseconds in a TimeSpan object.
The TotalDays, TotalHours, TotalMinutes, TotalSeconds, and TotalMilliseconds
properies return the totals of them on an object.
The following code snippet gets a TimeSpan between two dates
and reads these properties.
TimeSpan Methods
TimeSpan class
provides FromDays, FromHours, FromMinutes, FromSeconds, and FromMilliseconds
methods to create TimeSpan objects from days, hours, minutes, seconds, and
milliseconds respectively.
The following code snippet adds TimeSpan objects using the
Fromxxx methods.
The Add, Subtract, Multiply,
Divide, and Negate methods to adds, subtract, divide, multiply, and negate
TimeSpan objects.
The following code snippet is an example of how to add and subtract TimeSpan objects.
The Parse, ParseExact, TryParse, TryParseExact, and
TryFormat methods are used to parse and format TimeSpan objects into strings
and vice versa.
Comments
Post a Comment