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. Formatting Strings Containing Months and Years

Formatting Strings Containing Months and Years

Scheduled Pinned Locked Moved C#
csharpquestionlounge
6 Posts 4 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.
  • A Offline
    A Offline
    Adam Jasper
    wrote on last edited by
    #1

    I hope I am asking this in the correct forum but I couldn't find a more appropriate one and I am using C# :) I am receiving data from a number of different third parties. Each data item has a month as a string. This month comes in a variety of different formats; Aug-10, Aug 10, Aug 2010, Aug-2010, Aug10, Aug2010, August 2010, August-2010. You get the general idea. I want to transform this into a single format, Aug 2010. I need to do this for any combination of month and year. My solution at the moment is to separate the alphabetic part from the numeric part and then rebuild the string using the first three letters of the alphabetic part, add a space, add "20" if length of the numeric string is 2, then add the numeric string. Whilst this works, it's messy and I am not overly comfortable with it, there must be a better way. Anyone got any clever ideas they would like to share? Thanks in advance, Adam

    E P realJSOPR 3 Replies Last reply
    0
    • A Adam Jasper

      I hope I am asking this in the correct forum but I couldn't find a more appropriate one and I am using C# :) I am receiving data from a number of different third parties. Each data item has a month as a string. This month comes in a variety of different formats; Aug-10, Aug 10, Aug 2010, Aug-2010, Aug10, Aug2010, August 2010, August-2010. You get the general idea. I want to transform this into a single format, Aug 2010. I need to do this for any combination of month and year. My solution at the moment is to separate the alphabetic part from the numeric part and then rebuild the string using the first three letters of the alphabetic part, add a space, add "20" if length of the numeric string is 2, then add the numeric string. Whilst this works, it's messy and I am not overly comfortable with it, there must be a better way. Anyone got any clever ideas they would like to share? Thanks in advance, Adam

      E Offline
      E Offline
      Ennis Ray Lynch Jr
      wrote on last edited by
      #2

      Convert all input data into a date time type. Set the day to 1. You may then use standard formatting strings to display and change your date display at whim. To aid in deciphering input, add spacing and add the text, "1st" to the end and then try using System.DateTime.TryParse to see if it can understand the format. If not roll your own for each one that doesn't fit.

      Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

      1 Reply Last reply
      0
      • A Adam Jasper

        I hope I am asking this in the correct forum but I couldn't find a more appropriate one and I am using C# :) I am receiving data from a number of different third parties. Each data item has a month as a string. This month comes in a variety of different formats; Aug-10, Aug 10, Aug 2010, Aug-2010, Aug10, Aug2010, August 2010, August-2010. You get the general idea. I want to transform this into a single format, Aug 2010. I need to do this for any combination of month and year. My solution at the moment is to separate the alphabetic part from the numeric part and then rebuild the string using the first three letters of the alphabetic part, add a space, add "20" if length of the numeric string is 2, then add the numeric string. Whilst this works, it's messy and I am not overly comfortable with it, there must be a better way. Anyone got any clever ideas they would like to share? Thanks in advance, Adam

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

        Use regex for interpreting the date parts at least. Then any captured month could be substring'd (0,3) to reformat it. And, Ennis's suggestion would be good to be sure it is a valid month.

        A 1 Reply Last reply
        0
        • A Adam Jasper

          I hope I am asking this in the correct forum but I couldn't find a more appropriate one and I am using C# :) I am receiving data from a number of different third parties. Each data item has a month as a string. This month comes in a variety of different formats; Aug-10, Aug 10, Aug 2010, Aug-2010, Aug10, Aug2010, August 2010, August-2010. You get the general idea. I want to transform this into a single format, Aug 2010. I need to do this for any combination of month and year. My solution at the moment is to separate the alphabetic part from the numeric part and then rebuild the string using the first three letters of the alphabetic part, add a space, add "20" if length of the numeric string is 2, then add the numeric string. Whilst this works, it's messy and I am not overly comfortable with it, there must be a better way. Anyone got any clever ideas they would like to share? Thanks in advance, Adam

          realJSOPR Offline
          realJSOPR Offline
          realJSOP
          wrote on last edited by
          #4

          DateTime date = DateTime.Now;
          string dateStr = date.ToString("MMM yyyy");

          It ain't rocket science.

          .45 ACP - because shooting twice is just silly
          -----
          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
          -----
          "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

          A 1 Reply Last reply
          0
          • realJSOPR realJSOP

            DateTime date = DateTime.Now;
            string dateStr = date.ToString("MMM yyyy");

            It ain't rocket science.

            .45 ACP - because shooting twice is just silly
            -----
            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
            -----
            "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

            A Offline
            A Offline
            Adam Jasper
            wrote on last edited by
            #5

            Hi John, Thanks for the response, however, that's not my question. My question was whether there was a quick way to read all the different string formats for the month and outputting them in a unified way without having to do lots of string parsing. Thanks anyway, Adam

            1 Reply Last reply
            0
            • P puromtec1

              Use regex for interpreting the date parts at least. Then any captured month could be substring'd (0,3) to reformat it. And, Ennis's suggestion would be good to be sure it is a valid month.

              A Offline
              A Offline
              Adam Jasper
              wrote on last edited by
              #6

              Thanks, I will give it a go.

              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