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. Date sting formatting, the easy way.

Date sting formatting, the easy way.

Scheduled Pinned Locked Moved The Weird and The Wonderful
rubyquestioncareer
10 Posts 8 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
    Keith Barrow
    wrote on last edited by
    #1

    string dateStr = DateTime.Now.Day.ToString("00") + DateTime.Now.Month.ToString("00") + DateTime.Now.Year.ToString();
    string timeStr = DateTime.Now.TimeOfDay.Hours.ToString("00") + DateTime.Now.TimeOfDay.Minutes.ToString("00") + DateTime.Now.TimeOfDay.Seconds.ToString("00");
    string path = FileOutputDir + GetProviderName() + "-Summary-" + dateStr + "-" + timeStr + ".csv";

    Another gem, from the gem of a project I've inherited. Now:

    string path = string.Format("{0}{1}-Summary-{2}", FileOutputDir, GetProviderName(), DateTime.Now.ToString("ddMMyyyy-HHmmss") + ".csv");

    [Edit] I've now just discovered the method containing this code never gets called. Its a good job wwe don't have any unit tests, or I'de have to remove those too..... :-)

    CCC solved so far: 2 (including a Hard One!) 37!?!! - Randall, Clerks

    L A P V D 5 Replies Last reply
    0
    • K Keith Barrow

      string dateStr = DateTime.Now.Day.ToString("00") + DateTime.Now.Month.ToString("00") + DateTime.Now.Year.ToString();
      string timeStr = DateTime.Now.TimeOfDay.Hours.ToString("00") + DateTime.Now.TimeOfDay.Minutes.ToString("00") + DateTime.Now.TimeOfDay.Seconds.ToString("00");
      string path = FileOutputDir + GetProviderName() + "-Summary-" + dateStr + "-" + timeStr + ".csv";

      Another gem, from the gem of a project I've inherited. Now:

      string path = string.Format("{0}{1}-Summary-{2}", FileOutputDir, GetProviderName(), DateTime.Now.ToString("ddMMyyyy-HHmmss") + ".csv");

      [Edit] I've now just discovered the method containing this code never gets called. Its a good job wwe don't have any unit tests, or I'de have to remove those too..... :-)

      CCC solved so far: 2 (including a Hard One!) 37!?!! - Randall, Clerks

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Given the number of posts about DateTime and its formats posted here, I am totally unsurprised.

      1 Reply Last reply
      0
      • K Keith Barrow

        string dateStr = DateTime.Now.Day.ToString("00") + DateTime.Now.Month.ToString("00") + DateTime.Now.Year.ToString();
        string timeStr = DateTime.Now.TimeOfDay.Hours.ToString("00") + DateTime.Now.TimeOfDay.Minutes.ToString("00") + DateTime.Now.TimeOfDay.Seconds.ToString("00");
        string path = FileOutputDir + GetProviderName() + "-Summary-" + dateStr + "-" + timeStr + ".csv";

        Another gem, from the gem of a project I've inherited. Now:

        string path = string.Format("{0}{1}-Summary-{2}", FileOutputDir, GetProviderName(), DateTime.Now.ToString("ddMMyyyy-HHmmss") + ".csv");

        [Edit] I've now just discovered the method containing this code never gets called. Its a good job wwe don't have any unit tests, or I'de have to remove those too..... :-)

        CCC solved so far: 2 (including a Hard One!) 37!?!! - Randall, Clerks

        A Offline
        A Offline
        Argyle4Ever
        wrote on last edited by
        #3

        i get that a lot from ppl that "think" they know how to code... rather than taking the shorter quicker way to do it they take the longer, "Look at all the code ive wrote to look cool" way of doing it in the off chance it baffles with obsufacation...

        View my CodePlex Projects here -> http://www.codeplex.com/site/users/view/john\_crocker

        1 Reply Last reply
        0
        • K Keith Barrow

          string dateStr = DateTime.Now.Day.ToString("00") + DateTime.Now.Month.ToString("00") + DateTime.Now.Year.ToString();
          string timeStr = DateTime.Now.TimeOfDay.Hours.ToString("00") + DateTime.Now.TimeOfDay.Minutes.ToString("00") + DateTime.Now.TimeOfDay.Seconds.ToString("00");
          string path = FileOutputDir + GetProviderName() + "-Summary-" + dateStr + "-" + timeStr + ".csv";

          Another gem, from the gem of a project I've inherited. Now:

          string path = string.Format("{0}{1}-Summary-{2}", FileOutputDir, GetProviderName(), DateTime.Now.ToString("ddMMyyyy-HHmmss") + ".csv");

          [Edit] I've now just discovered the method containing this code never gets called. Its a good job wwe don't have any unit tests, or I'de have to remove those too..... :-)

          CCC solved so far: 2 (including a Hard One!) 37!?!! - Randall, Clerks

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

          string.Format("{0}{1}-Summary-{2:ddMMyyyy-HHmmss}.csv", ... But please use an ISO 8601 compliant format: yyyyMMdd

          K 1 Reply Last reply
          0
          • P PIEBALDconsult

            string.Format("{0}{1}-Summary-{2:ddMMyyyy-HHmmss}.csv", ... But please use an ISO 8601 compliant format: yyyyMMdd

            K Offline
            K Offline
            Keith Barrow
            wrote on last edited by
            #5

            Yep (& I should have spotted it :doh: ), except for the ISO 8061 bit, that's out of my hands...

            CCC solved so far: 2 (including a Hard One!) 37!?!! - Randall, Clerks

            1 Reply Last reply
            0
            • K Keith Barrow

              string dateStr = DateTime.Now.Day.ToString("00") + DateTime.Now.Month.ToString("00") + DateTime.Now.Year.ToString();
              string timeStr = DateTime.Now.TimeOfDay.Hours.ToString("00") + DateTime.Now.TimeOfDay.Minutes.ToString("00") + DateTime.Now.TimeOfDay.Seconds.ToString("00");
              string path = FileOutputDir + GetProviderName() + "-Summary-" + dateStr + "-" + timeStr + ".csv";

              Another gem, from the gem of a project I've inherited. Now:

              string path = string.Format("{0}{1}-Summary-{2}", FileOutputDir, GetProviderName(), DateTime.Now.ToString("ddMMyyyy-HHmmss") + ".csv");

              [Edit] I've now just discovered the method containing this code never gets called. Its a good job wwe don't have any unit tests, or I'de have to remove those too..... :-)

              CCC solved so far: 2 (including a Hard One!) 37!?!! - Randall, Clerks

              V Offline
              V Offline
              VentsyV
              wrote on last edited by
              #6

              OK I understand whats wrong with those:

              string dateStr = DateTime.Now.Day.ToString("00") + DateTime.Now.Month.ToString("00") + DateTime.Now.Year.ToString();
              string timeStr = DateTime.Now.TimeOfDay.Hours.ToString("00") + DateTime.Now.TimeOfDay.Minutes.ToString("00") + DateTime.Now.TimeOfDay.Seconds.ToString("00");

              I would have been easier to do: DateTime.Now.ToString("mmDDYYYY") or whatever the format string was. But I'm not quite sure what's wrong with these:

              string path = FileOutputDir + GetProviderName() + "-Summary-" + dateStr + "-" + timeStr + ".csv";
              string path = string.Format("{0}{1}-Summary-{2}", FileOutputDir, GetProviderName(), DateTime.Now.ToString("ddMMyyyy-HHmmss") + ".csv");

              D 1 Reply Last reply
              0
              • V VentsyV

                OK I understand whats wrong with those:

                string dateStr = DateTime.Now.Day.ToString("00") + DateTime.Now.Month.ToString("00") + DateTime.Now.Year.ToString();
                string timeStr = DateTime.Now.TimeOfDay.Hours.ToString("00") + DateTime.Now.TimeOfDay.Minutes.ToString("00") + DateTime.Now.TimeOfDay.Seconds.ToString("00");

                I would have been easier to do: DateTime.Now.ToString("mmDDYYYY") or whatever the format string was. But I'm not quite sure what's wrong with these:

                string path = FileOutputDir + GetProviderName() + "-Summary-" + dateStr + "-" + timeStr + ".csv";
                string path = string.Format("{0}{1}-Summary-{2}", FileOutputDir, GetProviderName(), DateTime.Now.ToString("ddMMyyyy-HHmmss") + ".csv");

                D Offline
                D Offline
                David Skelly
                wrote on last edited by
                #7

                I think that the OP is saying that he replaced the first example block of code with the second. The first block is what it used to do; the second block is what it does now. Although, apparently it never gets called anyway so the whole thing is academic.

                K 1 Reply Last reply
                0
                • D David Skelly

                  I think that the OP is saying that he replaced the first example block of code with the second. The first block is what it used to do; the second block is what it does now. Although, apparently it never gets called anyway so the whole thing is academic.

                  K Offline
                  K Offline
                  Keith Barrow
                  wrote on last edited by
                  #8

                  Exactly so!

                  CCC solved so far: 2 (including a Hard One!) 37!?!! - Randall, Clerks

                  1 Reply Last reply
                  0
                  • K Keith Barrow

                    string dateStr = DateTime.Now.Day.ToString("00") + DateTime.Now.Month.ToString("00") + DateTime.Now.Year.ToString();
                    string timeStr = DateTime.Now.TimeOfDay.Hours.ToString("00") + DateTime.Now.TimeOfDay.Minutes.ToString("00") + DateTime.Now.TimeOfDay.Seconds.ToString("00");
                    string path = FileOutputDir + GetProviderName() + "-Summary-" + dateStr + "-" + timeStr + ".csv";

                    Another gem, from the gem of a project I've inherited. Now:

                    string path = string.Format("{0}{1}-Summary-{2}", FileOutputDir, GetProviderName(), DateTime.Now.ToString("ddMMyyyy-HHmmss") + ".csv");

                    [Edit] I've now just discovered the method containing this code never gets called. Its a good job wwe don't have any unit tests, or I'de have to remove those too..... :-)

                    CCC solved so far: 2 (including a Hard One!) 37!?!! - Randall, Clerks

                    D Offline
                    D Offline
                    Dan Mos
                    wrote on last edited by
                    #9

                    That's nothing. Check this piece of pure art.

                        //returns a string representation of the date used to create
                        //the logs file. The logs file are somethnig like '15\_09\_2009.log'.        
                        //a new log file is created each day if and ONLY if an error
                        //apears while using the program that day.
                        public static string GetStringDate(DateTime dt)
                        {
                            StringBuilder sb = new StringBuilder();           
                    
                            //Month
                            int mnth = dt.Month;
                            if (mnth < 10)
                            {
                                //leading zero                
                                sb.Append('0');
                                sb.Append(mnth);
                                sb.Append('\_');
                            }
                            else
                            {                
                                sb.Append(mnth);
                                sb.Append('\_');
                            }
                            //day
                            int day = dt.Day;
                            if (day < 10)
                            {
                                //leading zero
                                sb.Append('0');
                                sb.Append(day);
                                sb.Append('\_');
                            }
                            else
                            {
                                sb.Append(day);
                                sb.Append('\_');
                            }
                            //year
                            sb.Append(dt.Year);
                    
                            //return the formated string
                            return sb.ToString();
                        }
                    

                    Among other spectacular utilities

                    T 1 Reply Last reply
                    0
                    • D Dan Mos

                      That's nothing. Check this piece of pure art.

                          //returns a string representation of the date used to create
                          //the logs file. The logs file are somethnig like '15\_09\_2009.log'.        
                          //a new log file is created each day if and ONLY if an error
                          //apears while using the program that day.
                          public static string GetStringDate(DateTime dt)
                          {
                              StringBuilder sb = new StringBuilder();           
                      
                              //Month
                              int mnth = dt.Month;
                              if (mnth < 10)
                              {
                                  //leading zero                
                                  sb.Append('0');
                                  sb.Append(mnth);
                                  sb.Append('\_');
                              }
                              else
                              {                
                                  sb.Append(mnth);
                                  sb.Append('\_');
                              }
                              //day
                              int day = dt.Day;
                              if (day < 10)
                              {
                                  //leading zero
                                  sb.Append('0');
                                  sb.Append(day);
                                  sb.Append('\_');
                              }
                              else
                              {
                                  sb.Append(day);
                                  sb.Append('\_');
                              }
                              //year
                              sb.Append(dt.Year);
                      
                              //return the formated string
                              return sb.ToString();
                          }
                      

                      Among other spectacular utilities

                      T Offline
                      T Offline
                      Teuz
                      wrote on last edited by
                      #10

                      Epic! Especially the sb.Append('_');

                      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