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 1 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 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
    Marc Clifton
    wrote on last edited by
    #21

    keefb wrote:

    I still can't work out why

    As to why, he only wants the date, with the time portion set to 0. ;) Marc

    Will work for food. Interacx

    I'm not overthinking the problem, I just felt like I needed a small, unimportant, uninteresting rant! - Martin Hart Turner

    M 1 Reply Last reply
    0
    • M Marc Clifton

      keefb wrote:

      I still can't work out why

      As to why, he only wants the date, with the time portion set to 0. ;) Marc

      Will work for food. Interacx

      I'm not overthinking the problem, I just felt like I needed a small, unimportant, uninteresting rant! - Martin Hart Turner

      M Offline
      M Offline
      Mark Hurd
      wrote on last edited by
      #22

      And I should add the simplest version is: DateTime.Today

      Regards, Mark Hurd, B.Sc.(Ma.) (Hons.)

      1 Reply Last reply
      0
      • N Nelson Costa Inacio

        Amazing bad code!!!!!!

        M Offline
        M Offline
        MaksimP
        wrote on last edited by
        #23

        // Try this: DateTime now   = DateTime.Now; DateTime date = new DateTime(now.Year, now.Month, now.Day);

        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
          Dave Parker
          wrote on last edited by
          #24

          Not good but I've seen worse. I'm sitting in front of code that's full of... try { // various code here } catch { } finally { } Empty catch and finally blocks everywhere. In your case though I think that will break if regional language settings define the date format as anything other than date/month/year as it'll format it with the date first and then reparse it as say mm/dd/yyyy in the US causing the date and month to be swapped.

          1 Reply 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.

            D Offline
            D Offline
            dojohansen
            wrote on last edited by
            #25

            Actually you're dead wrong in so many ways. :D 1) Since ToString() and Parse() both use the *same* culture, it doesn't matter what that culture is. 2) The culture used is not necessarily the system default. It is the current culture, which can be set programmatically to whatever we'd like it to be. 3) The default culture doesn't actually depend on where the machine is located. I once wrote a disposable class called CultureBubble in order to easily run portions of code with a specific culture, like this:

            using (new CultureBubble("FR"))
            {
            foo();
            bar();
            }

            This technique is quite useful, as it requires minimal coding compared to writing logic for dealing with different cultures everywhere data is parsed or presented in culture-dependent ways.

            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