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. Converting strings to DateTime structs

Converting strings to DateTime structs

Scheduled Pinned Locked Moved C#
questionlearning
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.
  • P Offline
    P Offline
    profoundwhispers
    wrote on last edited by
    #1

    I get dates in the following format: Mar-07 13:45 How can I feed this to a ConvertTo.DateTime expression without getting an exception? Sammy "A good friend, is like a good book: the inside is better than the cover..."

    E 1 Reply Last reply
    0
    • P profoundwhispers

      I get dates in the following format: Mar-07 13:45 How can I feed this to a ConvertTo.DateTime expression without getting an exception? Sammy "A good friend, is like a good book: the inside is better than the cover..."

      E Offline
      E Offline
      Edbert P
      wrote on last edited by
      #2

      Tough luck. I think you need to write your own function to read it and convert it to ISO format. Check if the format is always mmm-dd hh:nn. If it is, it will be simpler for you to write the function. I might be wrong though, so if anyone knows about this, feel free to answer. :) Edbert P. Sydney, Australia.

      P 1 Reply Last reply
      0
      • E Edbert P

        Tough luck. I think you need to write your own function to read it and convert it to ISO format. Check if the format is always mmm-dd hh:nn. If it is, it will be simpler for you to write the function. I might be wrong though, so if anyone knows about this, feel free to answer. :) Edbert P. Sydney, Australia.

        P Offline
        P Offline
        profoundwhispers
        wrote on last edited by
        #3

        Yes in fact, it is always in this format. How should I write this function? Sammy "A good friend, is like a good book: the inside is better than the cover..."

        E 1 Reply Last reply
        0
        • P profoundwhispers

          Yes in fact, it is always in this format. How should I write this function? Sammy "A good friend, is like a good book: the inside is better than the cover..."

          E Offline
          E Offline
          Edbert P
          wrote on last edited by
          #4

          I've checked that in your case you only need to add the year value to the string. Here's what I would do:

          string dateValue = "Mar-07 13:45";
          string[] dateParts = dateValue.Split(' ');
          dateValue = String.Concat(dateParts[0], "-", DateTime.Now.Year, " ", dateParts[1]);

          You can use the final string as input for any DateTime functions. Edbert P. Sydney, Australia.

          B 1 Reply Last reply
          0
          • E Edbert P

            I've checked that in your case you only need to add the year value to the string. Here's what I would do:

            string dateValue = "Mar-07 13:45";
            string[] dateParts = dateValue.Split(' ');
            dateValue = String.Concat(dateParts[0], "-", DateTime.Now.Year, " ", dateParts[1]);

            You can use the final string as input for any DateTime functions. Edbert P. Sydney, Australia.

            B Offline
            B Offline
            Bryan White
            wrote on last edited by
            #5

            A word of warning when reading December's data in January - it may have wrong year! Remember to add logic something like pseudocode: if(Month(data) > Month(Now)) DataYear -= 1; Regards Brewman

            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