Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. DateTime.TryParse

DateTime.TryParse

Scheduled Pinned Locked Moved C#
tutorial
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kjetil Svendsen
    wrote on last edited by
    #1

    Any idea how to convert strings like "Tuesday 1st April 2008" to a DateTime. I have tried DateTime.TryParse with different CultureInfo, without result Kjetil

    G 1 Reply Last reply
    0
    • K Kjetil Svendsen

      Any idea how to convert strings like "Tuesday 1st April 2008" to a DateTime. I have tried DateTime.TryParse with different CultureInfo, without result Kjetil

      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #2

      You can use DateTime.ParseExact Method [^]

      Giorgi Dalakishvili #region signature my articles #endregion

      K 1 Reply Last reply
      0
      • G Giorgi Dalakishvili

        You can use DateTime.ParseExact Method [^]

        Giorgi Dalakishvili #region signature my articles #endregion

        K Offline
        K Offline
        Kjetil Svendsen
        wrote on last edited by
        #3

        Thanks Useful tip, but I was not able parse the date DateTime dt = DateTime.ParseExact("April 1st 2008", "MMMM dd yyyy", new CultureInfo("en-US"), DateTimeStyles.None); //Failed DateTime dt = DateTime.ParseExact("April 1 2008", "MMMM dd yyyy", new CultureInfo("en-US"), DateTimeStyles.None); //Failed DateTime dt = DateTime.ParseExact("April 01 2008", "MMMM dd yyyy", new CultureInfo("en-US"), DateTimeStyles.None); //Succeeded Any idea what formatstring to use ? I could manipulate the string before parsing, but I'm parsing 8 different languages with different date format :-/ Kjetil

        D 1 Reply Last reply
        0
        • K Kjetil Svendsen

          Thanks Useful tip, but I was not able parse the date DateTime dt = DateTime.ParseExact("April 1st 2008", "MMMM dd yyyy", new CultureInfo("en-US"), DateTimeStyles.None); //Failed DateTime dt = DateTime.ParseExact("April 1 2008", "MMMM dd yyyy", new CultureInfo("en-US"), DateTimeStyles.None); //Failed DateTime dt = DateTime.ParseExact("April 01 2008", "MMMM dd yyyy", new CultureInfo("en-US"), DateTimeStyles.None); //Succeeded Any idea what formatstring to use ? I could manipulate the string before parsing, but I'm parsing 8 different languages with different date format :-/ Kjetil

          D Offline
          D Offline
          DaveyM69
          wrote on last edited by
          #4

          I don't think it's possible to have one generic format for the above examples. Either some manipulation and FormatException handling is going to be required, or you're going to have to force them in your UI to enter data in an acceptable format by either separating the day, month and year fields into separate controls or using a DateTimePicker or something similar. The second two examples shouldn't be too hard, but the date suffix in the first does not appear to be included in any of the custom format mechanisms - unless it's possible to use a wild card for the two characters? It may be easier to create your own control that can handle the 8 different language formats - that will give you more flexibility if more are required later.

          Dave

          K 1 Reply Last reply
          0
          • D DaveyM69

            I don't think it's possible to have one generic format for the above examples. Either some manipulation and FormatException handling is going to be required, or you're going to have to force them in your UI to enter data in an acceptable format by either separating the day, month and year fields into separate controls or using a DateTimePicker or something similar. The second two examples shouldn't be too hard, but the date suffix in the first does not appear to be included in any of the custom format mechanisms - unless it's possible to use a wild card for the two characters? It may be easier to create your own control that can handle the 8 different language formats - that will give you more flexibility if more are required later.

            Dave

            K Offline
            K Offline
            Kjetil Svendsen
            wrote on last edited by
            #5

            Guess I have to do some string-manipulation. My app reads the dates from different sources, like word, excel, text or xml files, so it's not possible to solve this by UI changes... Thanks Kjetil

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups