Saturday, March 13, 2010

TimeSpan Structure

In this post I will show you how you can use the TimeSpan structure, I know many of you have used it and know how to use the TimeSpan structure, But this post is for those who have never used it or for the beginner level readers.
A TimeSpan object represents a time interval (duration of time or elapsed time) that is measured as a positive or negative number of days, hours, minutes, seconds, and fractions of a second. The TimeSpan structure can also be used to represent the time of day, but only if the time is unrelated to a particular date. Otherwise, the DateTime or DateTimeOffset structure should be used instead.
For my example code (which I have develop desktop application using visual studio 2008) I have just tried to make simple time calculate which is used to Subtract the first date time value from the second date time value. For this I have two date time picker controls and two buttons one to calculate and second one to reset the values. Below is the image of the main form, which is taken by setting the difference of one day between two dates control. You can see in the example code that I have used the costomFormat of the date by setting the Format of the control to custom and I have used the dd/MM/yyyy HH:mm for both controls, So that time can also be displayed.



Here is my code which is used to assign the result to the TimeSpan object.

TimeSpan tsDateDifference = dtpSecondDate.Value.Subtract(dtpFirstDate.Value);
After you have set the value of the TimeSpan object here are the list property which you can use to get the result. And I have used all of them in my example code.

Property
Description
DaysGets the days component of the time interval represented by the current TimeSpan structure.
HoursGets the hours component of the time interval represented by the current TimeSpan structure.
MillisecondsGets the milliseconds component of the time interval represented by the current TimeSpan structure.
MinutesGets the minutes component of the time interval represented by the current TimeSpan structure.
SecondsGets the seconds component of the time interval represented by the current TimeSpan structure.
TicksGets the number of ticks that represent the value of the current TimeSpan structure.
TotalDaysGets the value of the current TimeSpan structure expressed in whole and fractional days.
TotalHoursGets the value of the current TimeSpan structure expressed in whole and fractional hours.
TotalMillisecondsGets the value of the current TimeSpan structure expressed in whole and fractional milliseconds.
TotalMinutesGets the value of the current TimeSpan structure expressed in whole and fractional minutes.
TotalSecondsGets the value of the current TimeSpan structure expressed in whole and fractional seconds.

Hope you will some idea of how to use the TimeSpan structure in you code when you need to have some calculation in your project and that calculation depends on Total hours , total minutes etc in your code. You can download the source code from here
All and any comments / bugs / suggestions are welcomed!

No comments: