locked
compare date RRS feed

  • Question

  • hi.. i tried this to compare the date

    if (System.DateTime.Compare(contact_birthday2.Text, contact_reminder2.Text) < 0)

     

    Error 1 The best overloaded method match for 'System.DateTime.Compare(System.DateTime, System.DateTime)' has some invalid arguments

    Error 2 Argument '1': cannot convert from 'string' to 'System.DateTime' 

     

    hope can expert to help here .. thanks

    Wednesday, March 12, 2008 2:31 PM

Answers

  • Hi alainkkk,

     

    From you description, you want to compare two date which is later, I have written a sample to demonstrate how you can do that, please check the demo below.

     

    Code Snippet

                string textdate1 = "5/24/2007";

                string textdate2 = "1/14/2008";

     

                System.DateTime d1 = System.DateTime.Parse(textdate1);

                System.DateTime d2 = System.DateTime.Parse(textdate2);

     

                if (System.DateTime.Compare(d1, d2) < 0)

                    MessageBox.Show("date 2 is later then date 1");

     

     

     

     

    Regards,

    Xun

     

    Friday, March 14, 2008 12:33 PM

All replies

  • You are trying to pass string based values instead of DateTime ones.

    In your case the code should be as following:

    if (DateTime.Compare(DateTime.Parse(contact_birthday2.Text), DateTime.Parse(contact_reminder2.Text) < 0)

     

    Also I think you've picked up the wrong forum for the question.

     

    Wednesday, March 12, 2008 2:55 PM
  • You need to pass the parameters as DateTime objects.
    So you need to parse your input text into a DateTime format, is the input in a particular manner?

    If its a simple format then you can try the datTime parse function.

    Code Snippet

    System.DateTime.Compare(DateTime.Parse(contact_birthday2.Text), DateTime.Parse(contact_reminder2.Text)



    Try that, if you need to pass in a specific time format you will need to use the ParseExact method and pass in the string that represents your date i.e. "yyyy-MM-dd" etc.

    Hope this helps


    Wednesday, March 12, 2008 2:57 PM
  • thanks.. but why wrong forum ?  i am doing wpf project with c# code behind.

    you meant those code are not for wpf project ?

     

    i stil encounter problem

    Error 1 Operator '<' cannot be applied to operands of type 'System.DateTime' and 'int'

     

    my database date field is stored as string as i only want the date

    e.g.   1/14/2008

    Thursday, March 13, 2008 1:51 AM
  • I moved this tread from WPF forum to C# forum.

    Thanks
    Friday, March 14, 2008 4:11 AM
  • Hi alainkkk,

     

    From you description, you want to compare two date which is later, I have written a sample to demonstrate how you can do that, please check the demo below.

     

    Code Snippet

                string textdate1 = "5/24/2007";

                string textdate2 = "1/14/2008";

     

                System.DateTime d1 = System.DateTime.Parse(textdate1);

                System.DateTime d2 = System.DateTime.Parse(textdate2);

     

                if (System.DateTime.Compare(d1, d2) < 0)

                    MessageBox.Show("date 2 is later then date 1");

     

     

     

     

    Regards,

    Xun

     

    Friday, March 14, 2008 12:33 PM