Suave Nti
Suave Nti

Reputation: 3747

Convert Date Value to Specific Format in C#

How to convert any date format to a specified format in C#.

For example:

If Date Format is

14.11.2011 or 14/11/2011 

Looking for a conversion function which converts into

yyyy-MM-dd format like 2011-11-14

Upvotes: 2

Views: 29491

Answers (4)

Vinoth
Vinoth

Reputation: 1

string s = "May 29,2012";
DateTime dt;
DateTime.TryParse(s, out dt);

Response.Write(dt.ToString("MM/dd/yyyy"));

Upvotes: 0

tbt
tbt

Reputation: 748

You can use the DateTime.Parse or DateTime.ParseExact methods to parse the string into a DateTime then you can use the DateTime.ToString() to return the date in the new format. For standard formatting check this page for custom date formats this

Upvotes: 0

archil
archil

Reputation: 39501

Take a look at DateTime.ToString() method, Custom Date and Time Format Strings and Standard Date and Time Format Strings

string customFormattedDateTimeString = DateTime.Now.ToString("yyyy-MM-dd");

Upvotes: 3

Andrew Barber
Andrew Barber

Reputation: 40150

Easy peasy:

var date = DateTime.Parse("14/11/2011"); // may need some Culture help here
Console.Write(date.ToString("yyyy-MM-dd"));

Upvotes: 10

Related Questions