user144114
user144114

Reputation:

C# Datetime formatting

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!!!

Upvotes: 2

Views: 170

Answers (1)

Joel Coehoorn
Joel Coehoorn

Reputation: 416121

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.

Upvotes: 12

Related Questions