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

前端 未结 6 623
庸人自扰
庸人自扰 2020-12-03 06:41

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 h

相关标签:
6条回答
  • 2020-12-03 07:14

    This works for me:

    string myTime = DateTime.Parse("12/1/2011")
                            .ToString(CultureInfo.GetCultureInfo("en-GB").DateTimeFormat.ShortDatePattern);
    
    0 讨论(0)
  • 2020-12-03 07:15
    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 ^^

    0 讨论(0)
  • 2020-12-03 07:30
    var culture = new CultureInfo( "en-GB" );
    var dateValue = new DateTime( 2011, 12, 1 );
    var result = dateValue.ToString( "d", culture ) );
    
    0 讨论(0)
  • 2020-12-03 07:31
    DateTime dateValue;
    CultureInfo culture = CultureInfo.CurrentCulture;
    DateTimeStyles styles = DateTimeStyles.None;
    DateTime.TryParse(datetimestring,culture, styles, out dateValue);
    
    0 讨论(0)
  • 2020-12-03 07:31

    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);
    
    0 讨论(0)
  • 2020-12-03 07:37
    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;
    
        }
    
    0 讨论(0)
提交回复
热议问题