34

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.

BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
Lokesh
  • 829
  • 2
  • 14
  • 26
  • Does this answer your question? [DateTime and CultureInfo](https://stackoverflow.com/questions/13797727/datetime-and-cultureinfo) – rmjoia Feb 19 '20 at 12:49

6 Answers6

62
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 ^^

Kent
  • 2,952
  • 2
  • 25
  • 42
  • Sorry, this is not working.DateTimeFormatInfo dtfi = new DateTimeFormatInfo(); dtfi.ShortDatePattern = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern; dtfi.DateSeparator = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.DateSeparator; result = Convert.ToDateTime(result, dtfi).ToString(System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern); – Lokesh Apr 08 '11 at 03:57
  • Because when you set `dtfi.ShortDatePattern = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePat­tern;` it will get your current DatePattern in your machine, not the pattern of the original date string :) – Kent Apr 08 '11 at 04:02
  • If you set the region and language settings as English[uk], the short date pattern is dd/MM/yyyy. – Lokesh Apr 08 '11 at 04:04
  • {"String was not recognized as a valid DateTime."} – Can Ürek Jan 13 '15 at 09:16
10

This works for me:

string myTime = DateTime.Parse("12/1/2011")
                        .ToString(CultureInfo.GetCultureInfo("en-GB").DateTimeFormat.ShortDatePattern);
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
9
var culture = new CultureInfo( "en-GB" );
var dateValue = new DateTime( 2011, 12, 1 );
var result = dateValue.ToString( "d", culture ) );
Thomas
  • 63,911
  • 12
  • 95
  • 141
  • CultureInfo culture = new CultureInfo(System.Threading.Thread.CurrentThread.CurrentCulture.Name); System.DateTime dt = System.DateTime.Parse(result); result = dt.ToString("d", culture); I tried this but its not working. Can you let me know why? – Lokesh Apr 08 '11 at 04:01
5
DateTime dateValue;
CultureInfo culture = CultureInfo.CurrentCulture;
DateTimeStyles styles = DateTimeStyles.None;
DateTime.TryParse(datetimestring,culture, styles, out dateValue);
Phoenix Kyaw
  • 332
  • 1
  • 5
  • 17
  • Man you saved my day, was looking everywhere to try and determine how I can set CUltureInfo on a variable level (without altering CurrentCulture) and could not find it under Date DataType's properties, methods etc... .TryParse wasn't available under a typical Date datatypes extension methods. When you showed me DateTime.TryParse, I realized it's outside of the available extension methods for Date and DateTime. Any reason why they've done it like this? Since you can declare an empty date & set itself with dateString + CultureInfo if TryParse was available in an extension method to Date type!??? – Erx_VB.NExT.Coder Sep 15 '13 at 12:40
  • You should use TryParse method in DateTime Conversion for safely easily conversion. – Phoenix Kyaw Sep 16 '13 at 05:28
1
public static DateTime ConvertDateTime(string Date)
    {
        DateTime date=new DateTime();                        
        try
        {
            string CurrentPattern = Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;                
            string[] Split = new string[] {"-","/",@"\","."};
            string[] Patternvalue = CurrentPattern.Split(Split,StringSplitOptions.None);
            string[] DateSplit = Date.Split(Split,StringSplitOptions.None);
            string NewDate = "";
            if (Patternvalue[0].ToLower().Contains("d") == true && Patternvalue[1].ToLower().Contains("m")==true && Patternvalue[2].ToLower().Contains("y")==true)
            {
                NewDate = DateSplit[1] + "/" + DateSplit[0] + "/" + DateSplit[2];
            }
            else if (Patternvalue[0].ToLower().Contains("m") == true && Patternvalue[1].ToLower().Contains("d")==true && Patternvalue[2].ToLower().Contains("y")==true)
            {
                NewDate = DateSplit[0] + "/" + DateSplit[1] + "/" + DateSplit[2];
            }
            else if (Patternvalue[0].ToLower().Contains("y") == true && Patternvalue[1].ToLower().Contains("m")==true && Patternvalue[2].ToLower().Contains("d")==true)
            {
                NewDate = DateSplit[2] + "/" + DateSplit[0] + "/" + DateSplit[1];
            }
            else if (Patternvalue[0].ToLower().Contains("y") == true && Patternvalue[1].ToLower().Contains("d")==true && Patternvalue[2].ToLower().Contains("m")==true)
            {
                NewDate = DateSplit[2] + "/" + DateSplit[1] + "/" + DateSplit[0];
            }
            date = DateTime.Parse(NewDate, Thread.CurrentThread.CurrentCulture);
        }
        catch (Exception ex)
        {

        }
        finally
        {

        }

        return date;

    }
j0k
  • 22,600
  • 28
  • 79
  • 90
0

This works for me,

DateTimeFormatInfo usDtfi = new CultureInfo("en-US", false).DateTimeFormat;
DateTimeFormatInfo ukDtfi = new CultureInfo("en-GB", false).DateTimeFormat;
string result = Convert.ToDateTime("26/09/2015",ukDtfi).ToString(usDtfi.ShortDatePattern);
vipin
  • 357
  • 3
  • 9