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. How to get today's date

How to get today's date

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharprubytutorial
25 Posts 15 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

    I've inherited one of the worst code-bases I've ever seen in 9 years of .netting. I've spotted this little gem scattered like gingerbread-crumbs thoughout the code:

    DateTime date = DateTime.Parse(DateTime.Now.Date.ToString("dd/MM/yyyy"));

    To make it worse this code is repeated not just in different classes but in the same class :wtf: To add piquancy, the variable being set is just called date, not today or todaysDate or something bit more sensible, so I had to work out what it did. I still can't work out why, I only know it makes my eyes bleed....

    N D C L M 8 Replies Last reply
    0
    • K Keith Barrow

      I've inherited one of the worst code-bases I've ever seen in 9 years of .netting. I've spotted this little gem scattered like gingerbread-crumbs thoughout the code:

      DateTime date = DateTime.Parse(DateTime.Now.Date.ToString("dd/MM/yyyy"));

      To make it worse this code is repeated not just in different classes but in the same class :wtf: To add piquancy, the variable being set is just called date, not today or todaysDate or something bit more sensible, so I had to work out what it did. I still can't work out why, I only know it makes my eyes bleed....

      N Offline
      N Offline
      Nelson Costa Inacio
      wrote on last edited by
      #2

      Amazing bad code!!!!!!

      M 1 Reply Last reply
      0
      • K Keith Barrow

        I've inherited one of the worst code-bases I've ever seen in 9 years of .netting. I've spotted this little gem scattered like gingerbread-crumbs thoughout the code:

        DateTime date = DateTime.Parse(DateTime.Now.Date.ToString("dd/MM/yyyy"));

        To make it worse this code is repeated not just in different classes but in the same class :wtf: To add piquancy, the variable being set is just called date, not today or todaysDate or something bit more sensible, so I had to work out what it did. I still can't work out why, I only know it makes my eyes bleed....

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

        One thing it will do is break if you run it in America. If the date is 5th March 2009, the ToString conversion will give you 05/03/2009. But because no format is specified on the Parse method, it will assume the default date format, which in America is MM/dd/yyyy. So 05/03/2009 will get converted to 3rd May 2009. My guess would be this is someone from a Java background because java.util.Date doesn't have an equivalent to .NET's DateTime.Date property, and it's not so easy to strip off the time part. The correct way to do it in Java is with java.util.Calendar, but lots of people use this sort of clumsy format/parse approach.

        K D 2 Replies Last reply
        0
        • D David Skelly

          One thing it will do is break if you run it in America. If the date is 5th March 2009, the ToString conversion will give you 05/03/2009. But because no format is specified on the Parse method, it will assume the default date format, which in America is MM/dd/yyyy. So 05/03/2009 will get converted to 3rd May 2009. My guess would be this is someone from a Java background because java.util.Date doesn't have an equivalent to .NET's DateTime.Date property, and it's not so easy to strip off the time part. The correct way to do it in Java is with java.util.Calendar, but lots of people use this sort of clumsy format/parse approach.

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

          Yup, a java-based programmer was involved, I did suspect something like that (there is lots of other Java-inspired "Goodness" in the code). I would have excused him, but he did manage to use DateTime.Now.Date as part of this coding beauty. Ho hum.

          L D 2 Replies Last reply
          0
          • K Keith Barrow

            Yup, a java-based programmer was involved, I did suspect something like that (there is lots of other Java-inspired "Goodness" in the code). I would have excused him, but he did manage to use DateTime.Now.Date as part of this coding beauty. Ho hum.

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Tell him about properties; people unfamiliar with Windows are propably not familiar with properties either. Java didn't have them when I used it. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


            D 1 Reply Last reply
            0
            • K Keith Barrow

              Yup, a java-based programmer was involved, I did suspect something like that (there is lots of other Java-inspired "Goodness" in the code). I would have excused him, but he did manage to use DateTime.Now.Date as part of this coding beauty. Ho hum.

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

              It happens both ways round. Because the syntax for Java and C# is very similar, it's easy to switch between the two languages and end up coding things inappropriately.

              1 Reply Last reply
              0
              • L Luc Pattyn

                Tell him about properties; people unfamiliar with Windows are propably not familiar with properties either. Java didn't have them when I used it. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


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

                Java still does not have properties. There was a big argument about whether they should be added to Java 7 or not (the next version due next year) and in the end they were not included. Personally, I don't miss properties in Java, possibly because I write mostly server-side Java apps, and properties are more useful for simple binding to UI controls. You can do that with Java beans and Swing but it's a bit messy.

                1 Reply Last reply
                0
                • K Keith Barrow

                  I've inherited one of the worst code-bases I've ever seen in 9 years of .netting. I've spotted this little gem scattered like gingerbread-crumbs thoughout the code:

                  DateTime date = DateTime.Parse(DateTime.Now.Date.ToString("dd/MM/yyyy"));

                  To make it worse this code is repeated not just in different classes but in the same class :wtf: To add piquancy, the variable being set is just called date, not today or todaysDate or something bit more sensible, so I had to work out what it did. I still can't work out why, I only know it makes my eyes bleed....

                  C Offline
                  C Offline
                  Carl B Johnson
                  wrote on last edited by
                  #8

                  Ha, I come from a Java background so my first thought was "What's wrong with it" :) I switched over to .NET about 7 years ago. so now I would write it like this: DateTime.Now.Date.ToShortDateString()

                  D K 2 Replies Last reply
                  0
                  • C Carl B Johnson

                    Ha, I come from a Java background so my first thought was "What's wrong with it" :) I switched over to .NET about 7 years ago. so now I would write it like this: DateTime.Now.Date.ToShortDateString()

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

                    7 years of .NET experience and you don't see what's wrong with it? Or is there a joke in there that I'm missing? (Monday morning, brain not fully engaged yet)

                    C 1 Reply Last reply
                    0
                    • C Carl B Johnson

                      Ha, I come from a Java background so my first thought was "What's wrong with it" :) I switched over to .NET about 7 years ago. so now I would write it like this: DateTime.Now.Date.ToShortDateString()

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

                      Actually DateTime date = DateTime.Parse(DateTime.Now.Date.ToString("dd/MM/yyyy")); can be written as DateTime date = DateTime.Now.Date; No strings involved! You'll also notice that the original coder acutally had the "DateTime.Now.Date" bit, which they then cast to a string (in UK date format) that is subsequently parsed back to a DateTime.

                      L B 2 Replies Last reply
                      0
                      • D David Skelly

                        7 years of .NET experience and you don't see what's wrong with it? Or is there a joke in there that I'm missing? (Monday morning, brain not fully engaged yet)

                        C Offline
                        C Offline
                        Carl B Johnson
                        wrote on last edited by
                        #11

                        Drink some coffee, it was a joke :laugh:

                        1 Reply Last reply
                        0
                        • K Keith Barrow

                          Actually DateTime date = DateTime.Parse(DateTime.Now.Date.ToString("dd/MM/yyyy")); can be written as DateTime date = DateTime.Now.Date; No strings involved! You'll also notice that the original coder acutally had the "DateTime.Now.Date" bit, which they then cast to a string (in UK date format) that is subsequently parsed back to a DateTime.

                          L Offline
                          L Offline
                          Luc Pattyn
                          wrote on last edited by
                          #12

                          In some parts of the world the original statement and yours will not yield the same result. And the original may throw an exception, yours wouldn't. So we need to see the specs first. :)

                          Luc Pattyn [Forum Guidelines] [My Articles]


                          The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                          1 Reply Last reply
                          0
                          • K Keith Barrow

                            I've inherited one of the worst code-bases I've ever seen in 9 years of .netting. I've spotted this little gem scattered like gingerbread-crumbs thoughout the code:

                            DateTime date = DateTime.Parse(DateTime.Now.Date.ToString("dd/MM/yyyy"));

                            To make it worse this code is repeated not just in different classes but in the same class :wtf: To add piquancy, the variable being set is just called date, not today or todaysDate or something bit more sensible, so I had to work out what it did. I still can't work out why, I only know it makes my eyes bleed....

                            L Offline
                            L Offline
                            leppie
                            wrote on last edited by
                            #13

                            TimeSpan keyDays = new TimeSpan(this.FooLicenceKeyHolder.FooLicence.Licence.LicenceExpiryDate.Ticks);
                            TimeSpan nowDays = new TimeSpan(System.DateTime.Now.Ticks);

                            int daysLeft = keyDays.Days - nowDays.Days;

                            xacc.ide
                            IronScheme - 1.0 beta 4 - out now!
                            ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

                            C D K 3 Replies Last reply
                            0
                            • L leppie

                              TimeSpan keyDays = new TimeSpan(this.FooLicenceKeyHolder.FooLicence.Licence.LicenceExpiryDate.Ticks);
                              TimeSpan nowDays = new TimeSpan(System.DateTime.Now.Ticks);

                              int daysLeft = keyDays.Days - nowDays.Days;

                              xacc.ide
                              IronScheme - 1.0 beta 4 - out now!
                              ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

                              C Offline
                              C Offline
                              Chris Meech
                              wrote on last edited by
                              #14

                              LOL. That example makes me want to fold. :)

                              Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]

                              L 1 Reply Last reply
                              0
                              • C Chris Meech

                                LOL. That example makes me want to fold. :)

                                Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]

                                L Offline
                                L Offline
                                leppie
                                wrote on last edited by
                                #15

                                Chris Meech wrote:

                                makes me want to fold.

                                Head-slamming the desk is a form of folding, not? (but seriously, that code still exists in our code base, not wrong, but funny ;P )

                                xacc.ide
                                IronScheme - 1.0 beta 4 - out now!
                                ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

                                1 Reply Last reply
                                0
                                • K Keith Barrow

                                  I've inherited one of the worst code-bases I've ever seen in 9 years of .netting. I've spotted this little gem scattered like gingerbread-crumbs thoughout the code:

                                  DateTime date = DateTime.Parse(DateTime.Now.Date.ToString("dd/MM/yyyy"));

                                  To make it worse this code is repeated not just in different classes but in the same class :wtf: To add piquancy, the variable being set is just called date, not today or todaysDate or something bit more sensible, so I had to work out what it did. I still can't work out why, I only know it makes my eyes bleed....

                                  M Offline
                                  M Offline
                                  mateotrek
                                  wrote on last edited by
                                  #16

                                  Simply amazing! Thanks for sharing, I had a good laugh.

                                  1 Reply Last reply
                                  0
                                  • L leppie

                                    TimeSpan keyDays = new TimeSpan(this.FooLicenceKeyHolder.FooLicence.Licence.LicenceExpiryDate.Ticks);
                                    TimeSpan nowDays = new TimeSpan(System.DateTime.Now.Ticks);

                                    int daysLeft = keyDays.Days - nowDays.Days;

                                    xacc.ide
                                    IronScheme - 1.0 beta 4 - out now!
                                    ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

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

                                    For a moment there I thought you were measuring your licence expiry in ticks. "This evaluation licence will expire in 342,827,400 nanoseconds."

                                    1 Reply Last reply
                                    0
                                    • L leppie

                                      TimeSpan keyDays = new TimeSpan(this.FooLicenceKeyHolder.FooLicence.Licence.LicenceExpiryDate.Ticks);
                                      TimeSpan nowDays = new TimeSpan(System.DateTime.Now.Ticks);

                                      int daysLeft = keyDays.Days - nowDays.Days;

                                      xacc.ide
                                      IronScheme - 1.0 beta 4 - out now!
                                      ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

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

                                      :wtf: :omg: :-D Yep, that takes the biscuit. What's the conversion rate of Idiotions into Imbiciles (our local dim-wit currency)?

                                      1 Reply Last reply
                                      0
                                      • K Keith Barrow

                                        Actually DateTime date = DateTime.Parse(DateTime.Now.Date.ToString("dd/MM/yyyy")); can be written as DateTime date = DateTime.Now.Date; No strings involved! You'll also notice that the original coder acutally had the "DateTime.Now.Date" bit, which they then cast to a string (in UK date format) that is subsequently parsed back to a DateTime.

                                        B Offline
                                        B Offline
                                        Brady Kelly
                                        wrote on last edited by
                                        #19

                                        Well spotted. Have some brick points.

                                        1 Reply Last reply
                                        0
                                        • K Keith Barrow

                                          I've inherited one of the worst code-bases I've ever seen in 9 years of .netting. I've spotted this little gem scattered like gingerbread-crumbs thoughout the code:

                                          DateTime date = DateTime.Parse(DateTime.Now.Date.ToString("dd/MM/yyyy"));

                                          To make it worse this code is repeated not just in different classes but in the same class :wtf: To add piquancy, the variable being set is just called date, not today or todaysDate or something bit more sensible, so I had to work out what it did. I still can't work out why, I only know it makes my eyes bleed....

                                          J Offline
                                          J Offline
                                          Joe Programm3r
                                          wrote on last edited by
                                          #20

                                          :omg: My jaw hurts from having it smack the desktop.

                                          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