Click here to Skip to main content
15,905,913 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
In my project(C# desktop app) i need to find the duration between starting time and ending time of a break. I use the following code

C#
string intrvl1 = stoptime.Subtract(starttime).ToString().Split('.')[0].ToString();


and i use following code to convert this string value to datetime
C#
DateTime intrvl = DateTime.Parse(intrvl1);


but when i convert string value 07:00 (which corresponds to 7 minute) i got 12:07:00

is there any other way to find duration and convert it to date time ?



Thanks in advance,
Posted
Comments
jaideepsinh 2-Aug-13 2:34am    
why you not find you time interval from sql from when you can fetch your start time?

 
Share this answer
 
Comments
Praveen_P 2-Aug-13 2:40am    
Thank you for solution , and i have another question how to save timespan in DB(sql server 2005)
jaideepsinh 2-Aug-13 2:50am    
In DB you can store time span as datatime field.
Thanks7872 2-Aug-13 2:44am    
Perfect. +5 !
Maciej Los 2-Aug-13 2:45am    
Thank you ;)
hi,
you should use DateDiff method for your purpose.

for eg.
SQL
SELECT DATEDIFF(DAY, GETDATE(), GETDATE() + 1) AS DayDiff
SELECT DATEDIFF(MINUTE, GETDATE(), GETDATE() + 1) AS MinuteDiff
SELECT DATEDIFF(SECOND, GETDATE(), GETDATE() + 1) AS SecondDiff
SELECT DATEDIFF(WEEK, GETDATE(), GETDATE() + 1) AS WeekDiff
SELECT DATEDIFF(HOUR, GETDATE(), GETDATE() + 1) AS HourDiff


and about how to convert to date time visit -

SQL Server Functions that helps to convert date and time values to and from string literals and other date and time formats.[^]
 
Share this answer
 
v2
Comments
ridoy 2-Aug-13 2:52am    
my 5!
Adarsh chauhan 2-Aug-13 2:53am    
Thanks..
Maciej Los 2-Aug-13 3:14am    
;)+5!
Adarsh chauhan 2-Aug-13 3:20am    
Thanks.. :)
Praveen_P 2-Aug-13 3:15am    
THANKS...........
 
Share this answer
 
v2
Comments
Adarsh chauhan 2-Aug-13 2:52am    
Perfect, I would also suggest same link (time span) .. :)
+5
ridoy 2-Aug-13 2:55am    
but you forgot to vote it :)
Adarsh chauhan 2-Aug-13 2:58am    
aah.. sorry :) here it is..
ridoy 2-Aug-13 3:00am    
Thanks Chauhan :)
DateTime startTime = Convert.ToDateTime("11 Aug 2013 13:10 PM");
DateTime endTime = Convert.ToDateTime("15 Sep 2013 16:20 PM");

//We subtract the time.
TimeSpan span = endTime.Subtract(startTime);

//This will just return the difference in Time
Response.Write("Time Difference (seconds): " + span.Seconds + Environment.NewLine);
Response.Write("Time Difference (minutes): " + span.Minutes + Environment.NewLine);
Response.Write("Time Difference (hours): " + span.Hours + Environment.NewLine);
Response.Write("Time Difference (days): " + span.Days + Environment.NewLine);
 
Share this answer
 
Comments
Adarsh chauhan 2-Aug-13 3:03am    
Good One... +5
Manish (Mack) 2-Aug-13 3:12am    
Thanks for +5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900