Click here to Skip to main content
15,917,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to subtract two time values which is stored in access 2007 as a text datatype

What I have tried:

String str1 = null;
           string str2 = null;
           cmd1.CommandText = "insert into TimeCalculation (AttendanceLogId,AttendanceDate,EmployeeId,InTime,OutTime,Duration) select distinct AttendanceLogId,AttendanceDate,EmployeeId,InTime,OutTime,@d from AttendanceLogs where NOT EXISTS ( select * from TimeCalculation where TimeCalculation.AttendanceLogId = AttendanceLogs.AttendanceLogId )";

           OleDbDataReader rd = cmd1.ExecuteReader();
           while (rd.Read())
               str1 = (rd["InTime"].ToString());
               str2 = (rd["OutTime"].ToString());
           TimeSpan time = DateTime.Parse(str2).Subtract(DateTime.Parse(str1));
           cmd1.Parameters.AddWithValue("@d", time.ToString());
Updated 14-Sep-17 20:33pm
Graeme_Grant 15-Sep-17 2:22am    
And the problem is? Why aren't dates stored in date format?

1 solution

Simple: Don't.
Store them as DateTime instead of strings, and you can immediately use standard comparison and math operations - making everything a whole load easier.

Always store data in relevant datatypes: anything else will always give you problems later on which are a huge amount worse than the trivial hassle it is to use the right types in the first place.
Share this answer

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