chathwind chathwind - 1 year ago 107
C# Question

Compare date string with current date?

I'm reading a date value from a sql data reader to a string data type variable like this.

electionPosDate = Convert.ToDateTime(reader["elecPODate"]).ToString("dd/MM/yyyy");

How can I compare it with current system date in an if condition ?



Answer Source

Use Date property of DateTime, if you want to check for Date part only, and for getting current date use DateTime.Now, but for only date part use DateTime.Now.Date:

DateTime electionPosDate = Convert.ToDateTime(reader["elecPODate"]);   
if(electionPosDate.Date ==DateTime.Now.Date)


See MSDN docs for Date and DateTime.Now

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download