locked
Compare Date strings RRS feed

  • Question

  • Hello

    I'm reading dates from a text file in the form of 08/05/2010 and I need to compare them to a date range that the user selected with the DateTimePicker control

    string startdate = dateTimePicker1.Value.ToShortDateString();

    string fiinishdate = dateTimePicker1.Value.ToShortDateString();

     

    Can i compare them as strings?  As I read a date from my file, do I also need to convert that as well?

     

    Thursday, August 5, 2010 2:46 PM

Answers

  • Hi,

    I would definitely compare these as DateTime and not as strings. You can parse the dates from the text file with the DateTime.TryParse methode, just like Mitja mentioned. The resulting DateTime can be used to compare it to the dateTimePicker1.Value and dateTimePicker2.Value.

    DateTime date;
    if (DateTime.TryParse(stringDate, out date))
    {
     // compare date
     bool success =
      date.CompareTo(dateTimePicker1.Value) > 0 &&
      date.CompareTo(dateTimePicker2.Value) < 0;
    }
    

    Hope this helps
    Meile Zetstra


    http://blogger.xs4all.nl/mzetstra

    • Marked as answer by Mike Dos Zhang Tuesday, August 10, 2010 10:05 AM
    Thursday, August 5, 2010 3:23 PM

All replies

  • And what exactly would you like to compare? would you like to get some data (for example: from database)? between these two dates?

    If so, you better use the DateTime, not a string values of dates:

        private void button1_Click(object sender, EventArgs e)
        {
          DateTime date1 = dateTimePicker1.Value;
          DateTime date2 = dateTimePicker2.Value;
        }
    

    • Proposed as answer by Konrad Neitzel Thursday, August 5, 2010 3:07 PM
    Thursday, August 5, 2010 2:51 PM
  • Yes compare them as DateTime. You can convert the strings from your textfile into DateTime with:

    Convert.ToDateTime("08/05/2010");

    Thursday, August 5, 2010 2:59 PM
  • .. and this might help you as well:

        private void button1_Click(object sender, EventArgs e)
        {
          //your text file:
          string strDate = "08/05/2010";
          //create a new date time and define it a 1.1. 1900( you have to define it somehow, becuase DateTime CANNOT be nul)
          //and later you can compare this time, like:
          //if the time is 1.1.1900 then the value (text) is not datetime!
          //this comes good if you insert values into database (if the value is 1.1.1900 is means the same as value is null).
          DateTime newTime = new DateTime(1900, 1, 1);
          //check if a string is a date:
          bool boolIsDateTime = false;
          boolIsDateTime= DateTime.TryParse(strDate, out newTime);
          if (boolIsDateTime)
          { 
           //if a string is an actual time value, do your code.
            DateTime date1 = dateTimePicker1.Value;
            DateTime date2 = dateTimePicker2.Value;
          }     
        }

    Thursday, August 5, 2010 2:59 PM
  • Hi,

    I would definitely compare these as DateTime and not as strings. You can parse the dates from the text file with the DateTime.TryParse methode, just like Mitja mentioned. The resulting DateTime can be used to compare it to the dateTimePicker1.Value and dateTimePicker2.Value.

    DateTime date;
    if (DateTime.TryParse(stringDate, out date))
    {
     // compare date
     bool success =
      date.CompareTo(dateTimePicker1.Value) > 0 &&
      date.CompareTo(dateTimePicker2.Value) < 0;
    }
    

    Hope this helps
    Meile Zetstra


    http://blogger.xs4all.nl/mzetstra

    • Marked as answer by Mike Dos Zhang Tuesday, August 10, 2010 10:05 AM
    Thursday, August 5, 2010 3:23 PM