How can I convert DateTime "Thu Nov 30 19:00:00 EST 2006" to "11/30/2006"
views:
112answers:
4
A:
DateTime.Parse() the string value than output to a custom format with dtVariable.ToString("custom string")
Josh Warner-Burke
2010-04-15 19:12:34
You mean `MM`. (Month, not minute)
SLaks
2010-04-15 19:17:51
mm for minutes. There should be MM
Hun1Ahpu
2010-04-15 19:18:44
`d` specifies "Short Date Format", which depending on the locale, may not be `mm/dd/yyyy`
mlsteeves
2010-04-15 19:19:12
@mlsteeves - You're right. But this is plus for globalization, isn't it?
Hun1Ahpu
2010-04-15 19:21:33
@mlsteeves: Which, in most cases, is even better - you wouldn't want someone whose culture writes `dd/mm/yyyy` to see `mm/dd/yyyy`...
BlueRaja - Danny Pflughoeft
2010-04-15 19:22:05
@BlueRaja Depends on what you are doing. If you are using an API that is expected mm/dd/yyyy, then 'd' might work during testing, but as soon as you go live, and it gets into an environment where 'd' != 'mm/dd/yyyy', then things start breaking.
mlsteeves
2010-04-16 12:20:51
+7
A:
Try something like this:
using System;
using System.Globalization;
class Example
{
static void Main()
{
DateTime dateTime = DateTime.ParseExact("Thu Nov 30 19:00:00 EST 2006",
"ddd MMM dd HH:mm:ss EST yyyy",
CultureInfo.InvariantCulture);
Console.WriteLine(dateTime.ToString("MM/dd/yyyy"));
}
}
The .NET framework does not support time zone abbreviations so I hard-coded "EST" into the format string (just something to be aware of if you will need to parse strings from multiple time zones).
Andrew Hare
2010-04-15 19:18:54