C# .NET - how to convert below datetime format value to timespan

Asked By mani on 31-May-12 06:10 AM

hi can any one tell me how to convert ( dt.ToString("HH:mm"); ) to timespan???


DateTime dt = DateTime.Parse(fixedtime);

            dt.ToString("HH:mm");
TSN ... replied to mani on 31-May-12 06:57 AM
hi,...

here are the various formats..


strDate = dt.ToString("HH:mm"); // 15:14
strDate = dt.ToString("hh:mm tt"); //
03:14 PM
strDate = dt.ToString("H:mm"); //
5:15
strDate = dt.ToString("h:mm tt"); // 3:16 PM
strDate = dt.ToString("HH:mm:ss"); //
15:16:29
joharika reddy replied to mani on 31-May-12 06:59 AM
Hi,
     Check this link http://asp-net-example.blogspot.in/2009/01/aspnet-date-time-example-how-to-use.html
Somesh Yadav replied to mani on 31-May-12 07:18 AM
If you want a timespan, you will need a start date and end date.

The code below shows how to get the timepan between current date and 11/11/2009.

TimeSpan oSpan = DateTime.Now - new DateTime(2009, 11, 11);
or
try {
DateTime dt1 = DateTime.Now;
DateTime dt2 = new DateTime(2003,4,15);
TimeSpan ts = dt1.Subtract(dt2);
Console.WriteLine(ts);
} catch (Exception ex) {
Console.WriteLine(ex.Message);
}