0

I would like to find total time of 2 DATETIME. Now I have 2 DATETIMES: loginDT and logoutDT. For loginDT I get from Database and logoutDT is now. like this code below

DateTime loginDT = (DateTime)readerS["login_Date_Time"];
DateTime logoutDT = DateTime.Now;
DateTime total = ??????????????

How should I do? Thanks All

Som Ctk
  • 1
  • 6

3 Answers3

0

Time intervals in C# are measured with TimeSpan structure. You can get it simply by substracting two DateTime variables:

DateTime loginDT = (DateTime)readerS["login_Date_Time"];
DateTime logoutDT = DateTime.Now;
TimeSpan total = logoutDT - loginDT;
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • Can I convert Time Span to Datetime again? because I have to store it to database again. – Som Ctk Oct 15 '15 at 07:36
  • TimeSpan is an interval of time - its like *2 hours*. It's distance on timeline. DateTime is a point on timeline (*13th October, 15:00*). You can add distance to some point on timeline and get another point (DateTime *13th October, 17:00*), but you cannot just convert distance to point. – Sergey Berezovskiy Oct 15 '15 at 07:38
  • Yes i think so using `Convert.ToDateTime(total.ToString());` But you have to alter as per your requirements. – Waqar Ahmed Oct 15 '15 at 07:38
  • @Som Ctk: probably you want `Double result = total.TotalDays;` (or alike: `total.TotalHours`, `total.TotalSeconds` etc.) to store the difference in days in the RDBMS. – Dmitry Bychenko Oct 15 '15 at 07:41
  • @SomCtk just store second DateTime `logout_Date_Time` in database. Later you can use [`DATEDIFF`](http://www.w3schools.com/sql/func_datediff.asp) sql function to query for time intervals. – Sergey Berezovskiy Oct 15 '15 at 07:45
  • So, If I write like this , it it correct? SQLiteDataReader readerS = comms.ExecuteReader(); DateTime loginDT = (DateTime)readerS["login_Date_Time"]; DateTime logoutDT = DateTime.Now; TimeSpan total = loginDT - logoutDT; string cmdLogout = "INSERT INTO Login_details (logout_Date_Time,totalTime_of_Login) VALUES ('" + logoutDT + "','" + total + "','" + "');"; ` – Som Ctk Oct 15 '15 at 07:47
  • @SomCtk no, just use two columns `login_Date_Time` and `logout_Date_Time` and DO NOT store interval in database. Save as names say - time. If you want to get login interval, then query `SELECT DATEDIFF(mi, login_Date_Time, logout_Date_Time) AS TotalLoginTime FROM Login_details` - that will return total login time in minutes. Also please use [parametrized queries](http://stackoverflow.com/questions/7505808/using-parameters-in-sql-statements) – Sergey Berezovskiy Oct 15 '15 at 07:52
  • @SergeyBerezovskiy Yeah, but anyway I have to store the login interval to DB. how should I do? – Som Ctk Oct 15 '15 at 07:57
  • @SomCtk ok, if you really need it, then create column of `bigint` type and store `total.Ticks` there. After reading data from database you can get interval back with `TimeSpan.FromTicks(db_value)` – Sergey Berezovskiy Oct 15 '15 at 08:02
0
 DateTime loginDT = (DateTime)readerS["login_Date_Time"];
 DateTime logoutDT = DateTime.Now;
 TimeSpan difference = loginDT.Subtract(logoutDT);
Waqar Ahmed
  • 1,414
  • 2
  • 15
  • 35
0

It seems, that you want something like that:

  DateTime loginDT = (DateTime)readerS["login_Date_Time"];
  DateTime logoutDT = DateTime.Now;

  //TODO: have look at TotalHours, TotalSeconds, TotalMilliseconds 
  // Difference in Days; 
  // store this value as a Number field of the RDBMS table
  Double totalDays = (logoutDT - loginDT).TotalDays;
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215