Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Converting time formats in C#

Options
  • 10-03-2006 4:04pm
    #1
    Closed Accounts Posts: 181 ✭✭


    Ok I have a problem where I have am trying to compare two dates. The first time is declared as
    DateTime CurDate = DateTime.Today;
    
    string todaydate = CurDate.ToShortTimeString();
    

    and it is the format DD/MM/YYYY

    and the second time values is in the form of a smalldatetime which is stored in a Sql server database

    It is declared in the program as
    string DataDate = (dr["Date"].ToString());
    

    and is in the format MM/DD/YYYY,

    what I want to be able to do is convert either one to others ones format so the check will be correct.

    I have tired changing the format from DateTime.Today to smalldatetostring() but nothing and I have also tired playing around with the DateTime function but I cant seem to get it.

    Anybody have any ideas?


Comments

  • Closed Accounts Posts: 24 Phileas Fogg


    Convert them both to DateTime instead of strings.
    DateTime todaydate = DateTime.Today.ToShortTimeString();
    
    System.Globalization.DateTimeFormatInfo dtfi = new System.Globalization.DateTimeFormatInfo();
    dtfi.MonthDayPattern = "dd/mm";
    
    DateTime DataDate = Convert.ToDateTime(dr["Date"].ToString(), dtfi);
    
    // NOw todayDate and DataDate can be compared i.e.
    
    if(todayDate > DataDate)
    {
       // do some stuff
    }
    
    
    
    


Advertisement