convert string 5/15/2008 to date time 15/5/2008 in c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • spellstone
    New Member
    • May 2008
    • 1

    convert string 5/15/2008 to date time 15/5/2008 in c#

    hello all aim a newbie in c# please tell me

    how shall i convert string 5/15/2008 to date time 15/5/2008 in c#
    and copmare the same with 14/5/2008
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Originally posted by spellstone
    hello all aim a newbie in c# please tell me

    how shall i convert string 5/15/2008 to date time 15/5/2008 in c#
    and copmare the same with 14/5/2008
    Usually dates are changed around based on the user's Culture.

    If your culture is set to en-US then your dates will be displayed:
    "mm/dd/yyyy"

    Whereas if your culture is set to fr-FR or es-ES then your dates will be displayed:
    "dd/mm/yyyy"

    By setting your user's culture you can format the way things are displayed...
    [code=vbnet]Thread.CurrentT hread.CurrentUI Culture = New CultureInfo("en-US")
    Thread.CurrentT hread.CurrentCu lture = CultureInfo.Cre ateSpecificCult ure("en-US")[/code]

    Please note that the above code will not only display your dates in the format for en-US, it will also attempt to look for any resources indicated for en-US.

    If you want to force your date to always be in one format, despite the user's culture settings, use the DateTimeFormatI nfo object...

    You should also look into Globalization.


    -Frinny

    Comment

    • blackjack2150
      New Member
      • Feb 2007
      • 79

      #3
      You can also do it by hand and avoid any possible Format mismatches:

      Use the string Split() method to get the 3 components and then create a new DateTime object using the constructor that asks for 3 integers (date, month, year).

      To convert a string to int use Convert.ToInt32 () method.

      GL.

      Comment

      Working...