views:

61

answers:

5

Is there anything standard in C# to transform in to out. I don't mind ad hoc routine either.

string in = "1 February 2001"
string out = "2001/02/01"  
A: 

DateTime out = DateTime.Parse(in);

thekaido
+3  A: 
Console.WriteLine(DateTime.Parse("1 February 2001").ToString("yyyy/MM/dd"));
sipwiz
+1  A: 

This assumes you're always in the en-US culture.

 string in = "1 February 2001";
    DateTime dt = DateTime.Parse(in);
    string out = dt.ToString("yyyy/MM/dd");
Steve Danner
+1 for mentioning culture info... `.Parse(in, CultureInfo.InvariantCulture)` or `.Parse(in, CultureInfo.CreateSpecificCulture("en-US"))`
Tanzelax
A: 
 DateTime dt = 
  Convert.ToDateTime("1 February 2001", new CultureInfo("2001/02/01"));
Asad Butt
A: 

You will want to parse the date in exact format than relying on the culture

DateTime.ParseExact(dateString, "d MMMM yyyyy", CultureInfo.InvariantCulture).ToString("yyyy/MM/dd");
Fadrian Sudaman