Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a Datetime string to a current culture datetime string

Tags:

c#

I have string say "12/1/2011" in English US culture my current machine culture is English Uk which is "dd/mm/yyyy" format. How to convert the 12/1/2011 to 1/12/2011. I have tried the below format.

System.DateTime.Parse(result,System.Threading.Thread.CurrentThread.CurrentCulture)               .ToString(System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern) 

but i could not able to see any output.

-Lokesh.

like image 491
Lokesh Avatar asked Apr 08 '11 03:04

Lokesh


People also ask

How do I convert datetime to date?

To convert a datetime to a date, you can use the CONVERT() , TRY_CONVERT() , or CAST() function.

How do I convert a date to a string in Python?

To convert Python datetime to string, use the strftime() function. The strftime() method is a built-in Python method that returns the string representing date and time using date, time, or datetime object.


1 Answers

DateTimeFormatInfo usDtfi = new CultureInfo("en-US", false).DateTimeFormat; DateTimeFormatInfo ukDtfi = new CultureInfo("en-GB", false).DateTimeFormat; string result = Convert.ToDateTime("12/01/2011", usDtfi).ToString(ukDtfi.ShortDatePattern); 

This will do the trick ^^

like image 129
Kent Avatar answered Sep 23 '22 21:09

Kent