Link to home
Create AccountLog in
Avatar of amillyard
amillyardFlag for United Kingdom of Great Britain and Northern Ireland

asked on

compare datetime with string value

e.Row.Cells[1].Text value comes from the a datagrid (datetime formatted column)

trying to compare serverDateTime the datagrid datetime value --

if (e.Row.Cells[1].Text <= serverDateTime)

trying to find out if e.Row.Cells[1].Text  datetime, is less than serverDateTime (current date)
DateTime serverTime = service.GetServerTime();
string serverDateTime = serverTime.ToString("MMM dd, yyyy HH:mm:ss");
if (e.Row.Cells[1].Text <= serverDateTime)

Open in new window

Avatar of p_davis


i would suggest that you evaluate based on the datetime type instead of string
if(Convert.ToDateTime(e.Row.Cells[1].Text)<= serverTime)
Avatar of amillyard


if(Convert.ToDateTime(e.Row.Cells[1].Text)<= serverTime)

compiles fine - but is not filtering the date though unfortunately.... even todays date is being allowed through

have also tried:

if (Convert.ToDateTime(e.Row.Cells[3].Text) < serverTime)
check the values and see if they are in  the same format.
serverTime -- is untouched -- the standard datetime capture into db.

e.Row.Cells[3].Text has been formatted (as used visually within the datagrid) -- to : dd-MMM-yy
Avatar of p_davis

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
this worked fine :  if (Convert.ToDateTime(e.Row.Cells[3].Text).Date < serverTime.Date)

how do I check for a specific additional number of days?  like 7 for example.  (i.e. 1 week earlier than today)
if it is just for days you can

Int32 dayDiff = Convert.ToDateTime(e.Row.Cells[3].Text).Day - serverTime.Day;

that will return an int with the difference. you will have to handle in your code based on the knowledge that you have of the times and what possible values the will be.

ie what does a -7 mean versus a 7.
p_davis: excellent feedback -- many thanks :-)
np thanks for the points and the grade