DateTime truncation
-
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.
-
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.
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)
-
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)
-
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)
No, because I'm provided the format at runtime -- read from an XML file. The format could be any valid format for a DateTime.
-
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.
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 -
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/2001Yes, but that doesn't help in this case either.
-
No, because I'm provided the format at runtime -- read from an XML file. The format could be any valid format for a DateTime.
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.