I think the method used is good.
Regarding Conversion of
Date
from
string
, if the Parse method is used then
Formatting is influenced by properties of the current DateTimeFormatInfo object, which by default are derived from the Regional and Language Options item in Control Panel. One reason the Parse method can unexpectedly throw FormatException is if the current DateTimeFormatInfo.DateSeparator and DateTimeFormatInfo.TimeSeparator properties are set to the same value. as explained here
http://msdn.microsoft.com/en-us/library/1k1skd40.aspx[
^]
Hence, if the
Format
of date being displayed in the
TextBox
is fixed, let us say
dd/MM/yyyy
, then the
ParseExact
method explained here
http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx[
^] where
CultureInfo.InvariantCulture
is recommended in the Note for custom format, may be better as shown below
DateTime dt2 = DateTime.ParseExact("02/04/2012","dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
Further these two statements
int numberOfDays = ts.Days;
Label1.Text = numberOfDays.ToString();
can be combined as
Label1.Text = ts.Days.ToString();
if there is no other purpose for
numberOfDays
.