formatting date while querying a db

gal mor 141 Reputation points
2022-10-24T13:24:25.517+00:00

I want to run certain queries depends on my values but one date entry is different from the other one so condition cant be true..
what should I do?

253562-date1.png253585-date2.png

Developer technologies ASP.NET Other
SQL Server Other
Developer technologies C#
{count} votes

Accepted answer
  1. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2022-10-25T02:10:29.97+00:00

    Hi @gal mor ,
    The date format of your database should be DateTime, you'd better set the date format to DateTime, not string.

    DateTime oldDate =Convert.ToDateTime( dt.Rows[e.RowIndex].ItemArray[2]);       
    DateTime DATE =Convert.ToDateTime(created.Text);  
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


2 additional answers

Sort by: Most helpful
  1. AgaveJoe 30,126 Reputation points
    2022-10-24T18:34:42.697+00:00

    It appears the problem is comparing two completely different string representations of a date; 27-10-2022 and 27/10/2022 00:00:00. Either convert the strings to actual DateTime types and compare the datetime types or apply the same string format to each date. I recommend converting the date strings to a DateTime.

    Parse date and time strings in .NET


  2. Olaf Helper 47,436 Reputation points
    2022-10-25T04:36:40.07+00:00

    but one date entry is different from the other

    That are no dates, but strings and they have different content, so what do you expect as result?
    Use type safe programming instead all as string.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.