Dev
Dev

Reputation: 687

How can I compare DateTime.Now with MySQL DATETIME format?

Hello guys I have a MySQL database with some DATETIME values stored. I want to compare those values in C# with the DateTime.Now attribute. I just want to write:

if (DateTime.Now.ToString() < MySQL fetched DATETIME + 3 hours)

Thanks for all your tips

Upvotes: 0

Views: 459

Answers (1)

Soner G&#246;n&#252;l
Soner G&#246;n&#252;l

Reputation: 98740

Do not compare your DateTime values with their string representations. You should compare them exactly as their values.

Instead;

  1. Use MySqlDataReader or any other structure to get the values from your database.
  2. Use GetDateTime(int) method to get relevant Datetime value.

And compare them like

if(DateTime.Now < fetchedDateTime.AddHours(3))

By the way, you should be aware of the case against DateTime.Now.

Upvotes: 2

Related Questions