DateTime Globalization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Terry Olsen

    #1

    DateTime Globalization

    I'm a bit confused on how to make my app work in all cultures, particularly
    with parsing the date out of a string. I read dates from different web
    sites. The formats I've run into include 'yyyy.mm.dd', 'dd/mm/yyyy',
    'yyyy-mm-dd', 'dd-mmm-yyyy'.

    I'm using the following code to parse the date, which has worked for a lot
    of Regions but not all, such as Germany.

    Dim CI As System.Globaliz ation.CultureIn fo
    CI = System.Globaliz ation.CultureIn fo.GetCultureIn fo("en-US")
    Dim dt as String = <Date scraped from web page>
    dt = Date.Parse(dt.T rim, CI.DateTimeForm at).ToShortDate String

    When the Region is set to Germany, the Date.Parse line throws an exception.
    The Message is "String was not recognized as a valid DateTime." In this
    case, dt = "2007.03.03 "

    How can I make this code to produce a valid date string in any culture?

    Thanks.


  • Cor Ligthert [MVP]

    #2
    Re: DateTime Globalization

    Terry,

    On a webpage there is no globalization, I wish it was there. The only thing
    you can do is put your date in a mask.

    Germany has the same date time structure as Holland and most states of the
    EU, that is not including the seperator, by instance in Holland those are
    used in the way the head of the user is standing (as we say).

    In the world there are 3 main structures when it is not about the seperator

    dd mm yy (most used because it is almost complete the EU, Africa and India)
    yy dd mm (secondly used because it is by instance the way in China)
    mm dd yyyy(rarely used, only the USA and some Coca Cola cultures)

    Cor

    "Terry Olsen" <tolsen64@hotma il.comschreef in bericht
    news:uRW%23ydhs HHA.412@TK2MSFT NGP04.phx.gbl.. .
    I'm a bit confused on how to make my app work in all cultures,
    particularly with parsing the date out of a string. I read dates from
    different web sites. The formats I've run into include 'yyyy.mm.dd',
    'dd/mm/yyyy', 'yyyy-mm-dd', 'dd-mmm-yyyy'.
    >
    I'm using the following code to parse the date, which has worked for a lot
    of Regions but not all, such as Germany.
    >
    Dim CI As System.Globaliz ation.CultureIn fo
    CI = System.Globaliz ation.CultureIn fo.GetCultureIn fo("en-US")
    Dim dt as String = <Date scraped from web page>
    dt = Date.Parse(dt.T rim, CI.DateTimeForm at).ToShortDate String
    >
    When the Region is set to Germany, the Date.Parse line throws an
    exception. The Message is "String was not recognized as a valid DateTime."
    In this case, dt = "2007.03.03 "
    >
    How can I make this code to produce a valid date string in any culture?
    >
    Thanks.
    >
    >

    Comment

    • Patrice

      #3
      Re: DateTime Globalization

      Did you left intentionaly en-US ? I tried with de-de and it works. Also use
      a constant string perhaps to make sure you don't have white spaces or non
      printable characters n the string you are trying to parse...

      The following works fine here (.NET 2.0) :

      Dim CI As System.Globaliz ation.CultureIn fo
      CI = System.Globaliz ation.CultureIn fo.GetCultureIn fo("de-de")
      Dim dt As String = "2007.03.03 "
      dt = Date.Parse(dt.T rim, CI.DateTimeForm at).ToShortDate String


      ---
      Patrice

      "Terry Olsen" <tolsen64@hotma il.coma écrit dans le message de news:
      uRW%23ydhsHHA.4 12@TK2MSFTNGP04 .phx.gbl...
      I'm a bit confused on how to make my app work in all cultures,
      particularly with parsing the date out of a string. I read dates from
      different web sites. The formats I've run into include 'yyyy.mm.dd',
      'dd/mm/yyyy', 'yyyy-mm-dd', 'dd-mmm-yyyy'.
      >
      I'm using the following code to parse the date, which has worked for a lot
      of Regions but not all, such as Germany.
      >
      Dim CI As System.Globaliz ation.CultureIn fo
      CI = System.Globaliz ation.CultureIn fo.GetCultureIn fo("en-US")
      Dim dt as String = <Date scraped from web page>
      dt = Date.Parse(dt.T rim, CI.DateTimeForm at).ToShortDate String
      >
      When the Region is set to Germany, the Date.Parse line throws an
      exception. The Message is "String was not recognized as a valid DateTime."
      In this case, dt = "2007.03.03 "
      >
      How can I make this code to produce a valid date string in any culture?
      >
      Thanks.
      >
      >

      Comment

      Working...