locked
Comparing Dates using C# RRS feed

  • Question

  • Hi,

    I am tyring to compare two dates(result_date and collect_date) using C#. My input dates are strings in MMDDYYYY format that I am converting into dates.

    Here is my code snippet

    DateTime res_dt = Convert.ToDateTime(result_date);

    DateTime coll_dt = Convert.ToDateTime(collect_date);

     

    I am inputting 01062004 for result_date and 01072004 for collect_date and the program spits out the following exception

    System.Format Exception: : String was not recognized as a valid Date Time

    Could anyone tell me how I can compare the two dates and find the difference between them? In other words, I need to do an if  between them like so..

    if(res_dt < coll_dt)

    return false;

    Thanks very much!

    Praveen

     

     



     

    Tuesday, November 7, 2006 7:57 AM

Answers

  • Sven's code is missing the required 3rd argument, try:
      DateTime.ParseExact(s, "MMddyyyy", Application.CurrentCulture.DateTimeFormat);

    Tuesday, November 7, 2006 11:34 AM

All replies

  • Hi,

    use this:

    DateTime res_dt = DateTime.ParseExact(result_date, "MMddyyyy"); // upper MM is important!

    --
    SvenC

    Tuesday, November 7, 2006 8:21 AM
  • Sven's code is missing the required 3rd argument, try:
      DateTime.ParseExact(s, "MMddyyyy", Application.CurrentCulture.DateTimeFormat);

    Tuesday, November 7, 2006 11:34 AM