This topic is locked
[SOLVED]

 Compare dates

2/12/2019 2:54:44 PM
ASPRunner.NET General questions
T
Tim author

Hello,
I am selecting a date from the database, then if it is earlier than today I want to run some code. I can't seem to get the syntax correct for this. Here is what I have:
// Get info from DB

string sql = DB.PrepareSQL("select LastSynch from Promos where ID = :master.ID");

XVar rs = tDAL.CustomQuery(sql);

XVar data = CommonFunctions.db_fetch_array(rs);
// Now compare date to today

if (data["LastSynch"] < DateTime.Now) {

Do some stuff

}
When I do a build I get this error:
Operator '<' cannot be applied to operands of type 'runnerDotNet.XVar' and 'System.DateTime'
I have tried several ToString() methods, but I get an error that strings can't be evaluated for < , which makes sense. I need these to be dates. I get that. How do I get the value that is returned from the DB query to be in a data format so that I can compare it to the system date?
Thanks,

Tim

admin 2/12/2019

You need to convert data["LastSynch"] to string first and then to date. Something like this:

DateTime.Parse(data["LastSynch"].ToString())


Your Google search should be "c# string to datetime"

T
Tim author 2/13/2019

Thank you! I understood what was needed in concept, but I was unaware of DateTime.Parse. And my Google search of "asp.net c# variable to date" wasn't helpful. Thanks also for the Google search hint. I now know everything I ever wanted to know (and some) about strings to dates.
Thanks,

Tim