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. DateTime truncation

DateTime truncation

Scheduled Pinned Locked Moved The Weird and The Wonderful
comxml
7 Posts 5 Posters 9 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.
  • P Offline
    P Offline
    PIEBALDconsult
    wrote on last edited by
    #1

    Today I had to write something I consider a horror. It's very much like some similarly bad code that's been exposed here, but no other solution came immediately to mind... and it works. :-D Without going into detail, I have a form that dynamically creates its controls according to an XML document. So, if the document specifies "System.DateTime" a DateTimePicker will be instantiated and added to the form. I also decided to add the ability to control the format via the XML, by setting the CustomFormat property. All is well and good. Today I was writing one of these XML documents and needed a DateTime with format "yyyy-MM-dd", simple enough, works great. Until I realized that I also needed to truncate the DateTime (zero-out the time-of-day part in this case). Truncating a DateTime is no big deal; I even wrote a (poorly-received) article[^] on it. But that solution won't work well in this case; I wanted a way to truncate based on the provided format. After a little thought I had the solution, but it feels so dirty: :~

    mydate = System.DateTime.ParseExact ( mydate.ToString ( format ) , format , null ) ;

    I haven't tested it extensively; I'm afraid of what I might find.

    M realJSOPR 2 Replies Last reply
    0
    • P PIEBALDconsult

      Today I had to write something I consider a horror. It's very much like some similarly bad code that's been exposed here, but no other solution came immediately to mind... and it works. :-D Without going into detail, I have a form that dynamically creates its controls according to an XML document. So, if the document specifies "System.DateTime" a DateTimePicker will be instantiated and added to the form. I also decided to add the ability to control the format via the XML, by setting the CustomFormat property. All is well and good. Today I was writing one of these XML documents and needed a DateTime with format "yyyy-MM-dd", simple enough, works great. Until I realized that I also needed to truncate the DateTime (zero-out the time-of-day part in this case). Truncating a DateTime is no big deal; I even wrote a (poorly-received) article[^] on it. But that solution won't work well in this case; I wanted a way to truncate based on the provided format. After a little thought I had the solution, but it feels so dirty: :~

      mydate = System.DateTime.ParseExact ( mydate.ToString ( format ) , format , null ) ;

      I haven't tested it extensively; I'm afraid of what I might find.

      M Offline
      M Offline
      Mladen Jankovic
      wrote on last edited by
      #2

      PIEBALDconsult wrote:

      Until I realized that I also needed to truncate the DateTime (zero-out the time-of-day part in this case).

      Maybe I misunderstood you, but couldn't you just use mydate.Date?

      Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)

      L P 2 Replies Last reply
      0
      • M Mladen Jankovic

        PIEBALDconsult wrote:

        Until I realized that I also needed to truncate the DateTime (zero-out the time-of-day part in this case).

        Maybe I misunderstood you, but couldn't you just use mydate.Date?

        Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)

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

        I was just going to say the same, you beat me by 5 minutes :)

        xacc.ide - now with TabsToSpaces support
        IronScheme - 1.0 alpha 4a out now (29 May 2008)

        1 Reply Last reply
        0
        • M Mladen Jankovic

          PIEBALDconsult wrote:

          Until I realized that I also needed to truncate the DateTime (zero-out the time-of-day part in this case).

          Maybe I misunderstood you, but couldn't you just use mydate.Date?

          Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)

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

          No, because I'm provided the format at runtime -- read from an XML file. The format could be any valid format for a DateTime.

          M 1 Reply Last reply
          0
          • P PIEBALDconsult

            Today I had to write something I consider a horror. It's very much like some similarly bad code that's been exposed here, but no other solution came immediately to mind... and it works. :-D Without going into detail, I have a form that dynamically creates its controls according to an XML document. So, if the document specifies "System.DateTime" a DateTimePicker will be instantiated and added to the form. I also decided to add the ability to control the format via the XML, by setting the CustomFormat property. All is well and good. Today I was writing one of these XML documents and needed a DateTime with format "yyyy-MM-dd", simple enough, works great. Until I realized that I also needed to truncate the DateTime (zero-out the time-of-day part in this case). Truncating a DateTime is no big deal; I even wrote a (poorly-received) article[^] on it. But that solution won't work well in this case; I wanted a way to truncate based on the provided format. After a little thought I had the solution, but it feels so dirty: :~

            mydate = System.DateTime.ParseExact ( mydate.ToString ( format ) , format , null ) ;

            I haven't tested it extensively; I'm afraid of what I might find.

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

            You can do math on the date to zero-out the time, and then parse it out...

            DateTime dt = DateTime.Now;
            dt = dt.Subtract(new TimeSpan(0,dt.Hour,dt.Minute,dt.Second,dt.Millisecond));

            "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." - Jason Jystad, 10/26/2001

            P 1 Reply Last reply
            0
            • realJSOPR realJSOP

              You can do math on the date to zero-out the time, and then parse it out...

              DateTime dt = DateTime.Now;
              dt = dt.Subtract(new TimeSpan(0,dt.Hour,dt.Minute,dt.Second,dt.Millisecond));

              "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." - Jason Jystad, 10/26/2001

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

              Yes, but that doesn't help in this case either.

              1 Reply Last reply
              0
              • P PIEBALDconsult

                No, because I'm provided the format at runtime -- read from an XML file. The format could be any valid format for a DateTime.

                M Offline
                M Offline
                Megidolaon
                wrote on last edited by
                #7

                I did the same. Something along the lines of:

                if (bool.TryParse(someString, out checkBool))
                {
                someProp1 = checkBool
                someString = someProp1.ToString()
                }

                if (DateTime.TryParse(someString, out checkDate))
                {
                someProp2 = checkBool
                someString = DateTime.Parse(someProp2.ToString()).ToString()
                }

                It's part of a dynamic Property Grid. Since I couldn't find any type checking for Property Grids, I had to create my own. And without the above code, if you entered stuff like "trUE" it would remain like that. With the above code, the user entries are formatted and dates always have the same format as well.

                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