KentZhou
KentZhou

Reputation: 25553

how to conver date string from one format to another format in c#?

Suppose I have date string like mydate = "24-Jun-2011";
I want to convert it to another format "2011-06-24". What is the simple way to do this?

Upvotes: 2

Views: 7052

Answers (4)

Bueller
Bueller

Reputation: 2344

http://www.csharp-examples.net/string-format-datetime/ has a lot of different formatting options... This should work well for you.

Upvotes: 0

agent-j
agent-j

Reputation: 27913

DateTime.ParseExact("24-Jun-2011", "dd-MMM-yyyy").ToString ("yyyy-MM-dd")

See formats here at MSDN.

Upvotes: 7

Piotr Auguscik
Piotr Auguscik

Reputation: 3681

U can Parse it to DateTime and then using tostring + special format get what u need

Upvotes: 1

Ray
Ray

Reputation: 46575

The best way is to parse the string to a DateTime and then convert it to a string again.

Be sure to have a look at the documentation for DateTime.Parse, DateTime.TryParse and DateTime.ToString

DateTime.Parse(myDate).ToString("yyyy-MM-dd");

Upvotes: 19

Related Questions