Frank gagui
Frank gagui

Reputation: 73

Compare two dates in c# from Mysql

I have a column in database named tblreburial and a field reburialdate. The type is varchar and the format of the date is (yyyy/dd/mm).In c# I have this query:

 string query = "Select date_format(reburialdate,'%Y/%d/%m') as reburialdate from tblreburial";

in my While statement:

 while (myReader.Read())
{
  string date = (myReader["Reburialdate"].ToString())
  if(date == DateTime.Now.Tostring("yyyy/dd/MM")){
       //some statements
   }
}

I already tried many ways but I often get a String was not recognized as a valid DateTime.

Upvotes: 0

Views: 295

Answers (2)

Arjun Verano
Arjun Verano

Reputation: 11

Try the following

var date = DateTime.ParseExact(myReader["Reburialdate"].ToString(), 
                              "yyyy/dd/MM", 
                               CultureInfo.InvariantCulture);

if(date.Date == DateTime.Today){
  //some statements
}

Upvotes: 0

Ankit Vijay
Ankit Vijay

Reputation: 4118

You can convert the dataReader to DateTime and compare it with DateTime.Today

  var date = DateTime.Parse(myReader["Reburialdate"])
  if(date.Date == DateTime.Today) {
      //some statements
  }

You can also optionally pass format information to DateTime.Parse. Here is the MSDN documentation.

Upvotes: 1

Related Questions