Compare if two date fields are equal in C#

Asked

Viewed 1,421 times

1

I have a form and in that form I have two date fields, the campodata1 and completed by the user and the campodata2 and filled by the database, now I need a code to compare if these two fields are with the same information.

The fields are string same I am using in the form o masktext for data fields, I just need to compare to see if they are the same to do an action.

Follow an image of my form Tela como aparece

  • 1

    what types of your dates ? fields and what kind of application are you using ? have you tried anything?

  • The fields are string I’m using in the form the masktext for data fields, I just need to compare to see if they are equal to do an action.

  • edit your question with this information and give an example of two dates.

  • 1

    try the following (campodata1 - campodata2).TotalDays if the return is zero are equal, if it is greater than zero the campodata1 is higher than campodata2 and if the return is less than zero the date campodata1 is lower than that of campodata2.

  • That’s right, but the simplest comparison, I just need to compare if they’re the same...

3 answers

2

using System;

public class Example
{
   public static void Main()
   {
      DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0);
      DateTime date2 = new DateTime(2009, 8, 1, 12, 0, 0);
      int result = DateTime.Compare(date1, date2);
      string relationship;

      if (result < 0)
         relationship = "é mais nova que";
      else if (result == 0)
         relationship = "é igual a";         
      else
         relationship = "é depois de";

      Console.WriteLine("{0} {1} {2}", date1, relationship, date2);
   }
}

Source:https://msdn.microsoft.com/pt-br/library/system.datetime.compare(v=vs.110). aspx

2

The accepted solution will break the application whenever someone enters a wrong format, so it works:

if (!datetime.TryParse(CampoData1.text, out var data1)) //faz o tratamento de erro aqui
if (!datetime.TryParse(CampoData2.text, out var data2)) //faz o tratamento de erro aqui
//só pode ir pra frente aqui se não houve erro acima
if (data1 > data2) //faz algo para o maior
else if (data1 < data2) //faz algo para o menor
else //faz algo para o igual

I put in the Github for future reference.

-1


What you can do is simply convert the dates into format STRING for DATETIME and make a normal comparison:

DateTime data1 = Convert.ToDateTime(CampoData1.text);
DateTime data2 = Convert.ToDateTime(CampoData2.text);

if (data1 > data2)
{
    /*...*/
}
else if (data1 == data2)
{
    /*...*/
}
else if (data1 < data2)
{
    /*...*/
}
  • Thanks for the help

  • You’re welcome! If the answer helped, give positive to it and mark it as correct, to help other people who arrive at this question and see that it is correct.

  • 1

    Paul thanks for the help, but I have a problem went to test now with the blank data2 field and gave u error of exception, how can I treat this error when the data2 field is blank, you can help me.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.