tags:

views:

72

answers:

1

Hi,

I want to know if there is already a build-in C# method that enables me quickly formate a US-culture date (07/22/2009) to '20090722' string.

I am using .NET framework 2.0;

Thanks!!!

+11  A: 

Assuming you already have it as a datetime:

DateTime dt = DateTime.Today;
dt.ToString("yyyyMMdd");

If you have a en-US datetime string, you can parse it like this:

DateTime dt = DateTime.ParseExact("07/22/2009", "MM/dd/yyyy", CultureInfo.GetCultureInfo("en-US"));

See the section on MSDN on standard and custom DateTime format strings.

Joel Coehoorn
great! thanks! i love stackoverflow