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. Other Discussions
  3. The Weird and The Wonderful
  4. Formatting a "yyyyMMdd" string to "yyyy-MM-dd" in C#

Formatting a "yyyyMMdd" string to "yyyy-MM-dd" in C#

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharp
9 Posts 8 Posters 47 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.
  • V Offline
    V Offline
    Vikram A Punathambekar
    wrote on last edited by
    #1

    Method name (only) changed. It shouldn't even have been public in the first place. Even if you disregard the blatant coding guidelines violations, I think this is a shame.

    public string getFormattedDate(string date)
    {
    string concat ="";
    if (null != date)
    {
    int i;
    String dateTimeChar = new String(date.ToCharArray());
    for(i=0;i<(date.Length-4);i++)
    {
    concat = concat+ dateTimeChar[i];
    }
    concat = concat+ ",";
    for(i=4;i<(date.Length-2);i++)
    {
    concat = concat+ dateTimeChar[i];
    }
    concat = concat+ ",";
    for(i=6;i<(date.Length);i++)
    {
    concat = concat+ dateTimeChar[i];
    }

    	DateTime dt = DateTime.Parse(concat);   
    	concat = dt.ToString("yyyy-MM-dd"); 
    }
    return concat;
    

    }

    P M J P S 5 Replies Last reply
    0
    • V Vikram A Punathambekar

      Method name (only) changed. It shouldn't even have been public in the first place. Even if you disregard the blatant coding guidelines violations, I think this is a shame.

      public string getFormattedDate(string date)
      {
      string concat ="";
      if (null != date)
      {
      int i;
      String dateTimeChar = new String(date.ToCharArray());
      for(i=0;i<(date.Length-4);i++)
      {
      concat = concat+ dateTimeChar[i];
      }
      concat = concat+ ",";
      for(i=4;i<(date.Length-2);i++)
      {
      concat = concat+ dateTimeChar[i];
      }
      concat = concat+ ",";
      for(i=6;i<(date.Length);i++)
      {
      concat = concat+ dateTimeChar[i];
      }

      	DateTime dt = DateTime.Parse(concat);   
      	concat = dt.ToString("yyyy-MM-dd"); 
      }
      return concat;
      

      }

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Arrgggghhhhh - that's horrible. Let's ignore the fact that the method name is camel cased and hit a particular arrogant nasty. Unless you have a really, really, really good reason (and by that I mean that you can prove to me that you know me much much better than I know myself), don't presume to know how to format a date that I will like. I hate it when people force these cultural settings on me, and I have refused to use some software in the past because it has presented regional variations on me that I don't like because they are downright confusing.:mad: As an example, what date is this? 06/05/07 Is it the 6th May 2007, the 7th May 2006, the 5th of June 2007?

      Deja View - the feeling that you've seen this post before.

      M 1 Reply Last reply
      0
      • P Pete OHanlon

        Arrgggghhhhh - that's horrible. Let's ignore the fact that the method name is camel cased and hit a particular arrogant nasty. Unless you have a really, really, really good reason (and by that I mean that you can prove to me that you know me much much better than I know myself), don't presume to know how to format a date that I will like. I hate it when people force these cultural settings on me, and I have refused to use some software in the past because it has presented regional variations on me that I don't like because they are downright confusing.:mad: As an example, what date is this? 06/05/07 Is it the 6th May 2007, the 7th May 2006, the 5th of June 2007?

        Deja View - the feeling that you've seen this post before.

        M Offline
        M Offline
        Mike Dimmick
        wrote on last edited by
        #3

        It's a very good practice to ensure that any date strings you output to a file that will be consumed by another program have a fixed format. It doesn't much matter what that format is, but there's an ISO standard for yyyyMMdd, so that's what I normally use. (SQL Server is happy to consume that format and do so consistently whatever regional settings are applied).

        Stability. What an interesting concept. -- Chris Maunder

        P P 2 Replies Last reply
        0
        • V Vikram A Punathambekar

          Method name (only) changed. It shouldn't even have been public in the first place. Even if you disregard the blatant coding guidelines violations, I think this is a shame.

          public string getFormattedDate(string date)
          {
          string concat ="";
          if (null != date)
          {
          int i;
          String dateTimeChar = new String(date.ToCharArray());
          for(i=0;i<(date.Length-4);i++)
          {
          concat = concat+ dateTimeChar[i];
          }
          concat = concat+ ",";
          for(i=4;i<(date.Length-2);i++)
          {
          concat = concat+ dateTimeChar[i];
          }
          concat = concat+ ",";
          for(i=6;i<(date.Length);i++)
          {
          concat = concat+ dateTimeChar[i];
          }

          	DateTime dt = DateTime.Parse(concat);   
          	concat = dt.ToString("yyyy-MM-dd"); 
          }
          return concat;
          

          }

          M Offline
          M Offline
          Mike Dimmick
          wrote on last edited by
          #4

          Ick. The obvious solution is

          return date.Substring(0,4) + "-" + date.Substring(4,2) + "-" + date.Substring(6,2);

          Goes nowhere near the risk of DateTime.Parse doing something different in a different locale.

          Stability. What an interesting concept. -- Chris Maunder

          1 Reply Last reply
          0
          • M Mike Dimmick

            It's a very good practice to ensure that any date strings you output to a file that will be consumed by another program have a fixed format. It doesn't much matter what that format is, but there's an ISO standard for yyyyMMdd, so that's what I normally use. (SQL Server is happy to consume that format and do so consistently whatever regional settings are applied).

            Stability. What an interesting concept. -- Chris Maunder

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            Yes, for outputting to a file or other method of "data interchange" use ISO 8601, for output to a person, allow that person's culture to format the date.

            1 Reply Last reply
            0
            • V Vikram A Punathambekar

              Method name (only) changed. It shouldn't even have been public in the first place. Even if you disregard the blatant coding guidelines violations, I think this is a shame.

              public string getFormattedDate(string date)
              {
              string concat ="";
              if (null != date)
              {
              int i;
              String dateTimeChar = new String(date.ToCharArray());
              for(i=0;i<(date.Length-4);i++)
              {
              concat = concat+ dateTimeChar[i];
              }
              concat = concat+ ",";
              for(i=4;i<(date.Length-2);i++)
              {
              concat = concat+ dateTimeChar[i];
              }
              concat = concat+ ",";
              for(i=6;i<(date.Length);i++)
              {
              concat = concat+ dateTimeChar[i];
              }

              	DateTime dt = DateTime.Parse(concat);   
              	concat = dt.ToString("yyyy-MM-dd"); 
              }
              return concat;
              

              }

              J Offline
              J Offline
              John R Shaw
              wrote on last edited by
              #6

              Even ignoring the language, there are too many things wrong. The most important being that the characters are not being gathered based on their value.

              while( date[i] == ‘y’ )
              Concat += date[i++];

              Or something similar. I have know idea why the new String is even present, other than to waste time and memory. The given code is just asking for the call to pass the wrong format, so that it can throw an exception. In general, this code would receive a ‘D’ at best and an ‘F’ in any production code.

              INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

              1 Reply Last reply
              0
              • M Mike Dimmick

                It's a very good practice to ensure that any date strings you output to a file that will be consumed by another program have a fixed format. It doesn't much matter what that format is, but there's an ISO standard for yyyyMMdd, so that's what I normally use. (SQL Server is happy to consume that format and do so consistently whatever regional settings are applied).

                Stability. What an interesting concept. -- Chris Maunder

                P Offline
                P Offline
                peterchen
                wrote on last edited by
                #7

                I once got burned by a ASP/ access database site that would create utter crap when move from an english development to a german production server. (Or the other way round? I forget).


                We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
                My first real C# project | Linkify!|FoldWithUs! | sighist

                1 Reply Last reply
                0
                • V Vikram A Punathambekar

                  Method name (only) changed. It shouldn't even have been public in the first place. Even if you disregard the blatant coding guidelines violations, I think this is a shame.

                  public string getFormattedDate(string date)
                  {
                  string concat ="";
                  if (null != date)
                  {
                  int i;
                  String dateTimeChar = new String(date.ToCharArray());
                  for(i=0;i<(date.Length-4);i++)
                  {
                  concat = concat+ dateTimeChar[i];
                  }
                  concat = concat+ ",";
                  for(i=4;i<(date.Length-2);i++)
                  {
                  concat = concat+ dateTimeChar[i];
                  }
                  concat = concat+ ",";
                  for(i=6;i<(date.Length);i++)
                  {
                  concat = concat+ dateTimeChar[i];
                  }

                  	DateTime dt = DateTime.Parse(concat);   
                  	concat = dt.ToString("yyyy-MM-dd"); 
                  }
                  return concat;
                  

                  }

                  P Offline
                  P Offline
                  pbraun
                  wrote on last edited by
                  #8

                  I'm guessing using the .ToString([inserted quoted format here]) was too difficult. What a shame. Phil

                  1 Reply Last reply
                  0
                  • V Vikram A Punathambekar

                    Method name (only) changed. It shouldn't even have been public in the first place. Even if you disregard the blatant coding guidelines violations, I think this is a shame.

                    public string getFormattedDate(string date)
                    {
                    string concat ="";
                    if (null != date)
                    {
                    int i;
                    String dateTimeChar = new String(date.ToCharArray());
                    for(i=0;i<(date.Length-4);i++)
                    {
                    concat = concat+ dateTimeChar[i];
                    }
                    concat = concat+ ",";
                    for(i=4;i<(date.Length-2);i++)
                    {
                    concat = concat+ dateTimeChar[i];
                    }
                    concat = concat+ ",";
                    for(i=6;i<(date.Length);i++)
                    {
                    concat = concat+ dateTimeChar[i];
                    }

                    	DateTime dt = DateTime.Parse(concat);   
                    	concat = dt.ToString("yyyy-MM-dd"); 
                    }
                    return concat;
                    

                    }

                    S Offline
                    S Offline
                    Sylvester george
                    wrote on last edited by
                    #9

                    Superb HORROR

                    Regards, Sylvester G sylvester_g_m@yahoo.com

                    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