Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello Guys,i hope you will be fine,well my issue is I want to know how to subtract (minus) data grid view DateTime to system date and time actually I want result in hh mm ss am not pm or actual time I want to know if someone call me so how much time before he called me on my phone .
Thanks

What I have tried:

int i;
int j;
int y;
i =datagridview.currentrow.cell[4].value.tostring()
j = system.datetime.now.tostring();
y = (i - j)
textbox1.text = y.tostring;
Posted
Updated 16-Jun-17 3:33am
Comments
F-ES Sitecore 16-Jun-17 9:30am    
Don't process the rendered cell values as you have no guarantee what is in them, you need to access the underlying data the gridview is bound to. You can access this via the RowDataBound event, an example is here.

Hopefully the data will be in DateTime so you just subtract the two values and use .ToString on the result to get it in the format you want. Google "c# subtract datetime values" and I'm sure you'll find examples.

1 solution

Stop converting to strings, and convert to DateTime instead:
If your value in the DatagridView is a DateTime, then use it directly:
C#
DateTime i = (DateTime) datagridview.currentrow.cell[4].value;

If it's a string, then Parse it:
C#
DateTime i;
if (!DateTime.TryParse(datagridview.currentrow.cell[4].value.ToString(), out i))
   {
   // Report a problem to user or log it
   ...
   return;
   }
Then you can just subtract:
C#
Timespan diff = i - DateTime.Now;
textbox1.Text = diff.Days.ToString();
If you don't want days, then there are other options: TimeSpan Properties (System)[^]
 
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