Don't underestimate the power of ToString(IFormatProvider)
UPDATE: I write this update minus one pinky finger. In the comments RichB and Paul van Brenk pointed out two bugs in my solution. First, RichB points out that ':' is a format modifier itself that describes the hours separator . So, that needs to be escaped. It can be escape either like \: or ':'. Paul points out that the final call to ToString should use the InvariantCulture, lest I end up with odd characters. Since I used the double (MM) numeric formats, this isn't a problem in 99% of cultures, but I could change calendars inadvertantly in Thai, in the Maldives, or in Saudi Arabia where different calendars are used.
Do NOT underestimate the power of ToString(IFormatProvider). I caught a fellow trying to to a DateTime conversion recently. He had do a (culture senstive) DateTime.ToString() then started parsing it into the specific format he wanted.
This is the height of pure evil and anyone who does it should lose a pinky. It's harder to parse these strings minus a little finger, and while harsh, this kind of punishment should deter others from making the same mistake.
The original question:
I have a DateTime variable which is holding the value as “1/13/2005 1:29:54 PM” and I want to convert to “20050113132954.000[0:GMT]”.
An answer:
DateTime foo = new DateTime(2005,1,13,13,29,54);
//TOTALLY OPTIONAL, but worth noting, assuming it was local time
DateTime bar = foo.ToUniversalTime();
string myNewDate = bar.ToString(@"yyyyMMddHHmmss.000[0\:G\MT]",System.Globalization.CultureInfo.InvariantCulture);
Notice the escaping on the "M" in the string literal "GMT." That's because "M" by itself is a Month formatter, so we say "no seriously, I meant M" by using "\M". Also, remember that "HH" is "zero-prepended 24-hour time" while "H" is "no-zeros 24-hour time" and "hh" and "h" are 12-hour times respectively. There are similar rules with "MM" for months and "mm" for minutes.
The final tip, don't even bother trying to do this stuff from memory without Chris Sells' FormatDesigner.
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.
About Newsletter
Perhaps we should be fortunate that Windows doesn't support the French Revolutionary calendar.
Comments are closed.
So, ToString(IFormatProvider) _is_ powerful, but it is so "powerful" it can even hurt experts.
(The answer is to enclose the colon in single quotes)