Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Converting time formats in C#

  • 10-03-2006 04: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