【问题标题】:How to convert a Datetime string to a current culture datetime string如何将日期时间字符串转换为当前区域性日期时间字符串
【发布时间】:2011-04-08 03:43:15
【问题描述】:

我在美国英语文化中有字符串说“12/1/2011”我当前的机器文化是英国英国,它是“dd/mm/yyyy”格式。如何将 12/1/2011 转换为 1/12/2011。我尝试了以下格式。

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

但我无法看到任何输出。

-洛克什。

【问题讨论】:

标签: c#


【解决方案1】:
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);

这样就可以了^^

【讨论】:

  • 对不起,这不起作用。DateTimeFormatInfo dtfi = new DateTimeFormatInfo(); dtfi.ShortDatePattern = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern; dtfi.DateSeparator = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.DateSeparator;结果 = Convert.ToDateTime(result, dtfi).ToString(System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern);
  • 因为当您设置dtfi.ShortDatePattern = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePat­tern; 时,它会在您的机器中获取您当前的 DatePattern,而不是原始日期字符串的模式 :)
  • 如果您将地区和语言设置为English[uk],则短日期模式为dd/MM/yyyy。
  • {"字符串未被识别为有效的日期时间。"}
【解决方案2】:

这对我有用:

string myTime = DateTime.Parse("12/1/2011")
                        .ToString(CultureInfo.GetCultureInfo("en-GB").DateTimeFormat.ShortDatePattern);

【讨论】:

    【解决方案3】:
    var culture = new CultureInfo( "en-GB" );
    var dateValue = new DateTime( 2011, 12, 1 );
    var result = dateValue.ToString( "d", culture ) );
    

    【讨论】:

    • CultureInfo 文化 = 新 CultureInfo(System.Threading.Thread.CurrentThread.CurrentCulture.Name); System.DateTime dt = System.DateTime.Parse(result);结果 = dt.ToString("d", 文化);我试过这个,但它不工作。你能告诉我为什么吗?
    【解决方案4】:
    DateTime dateValue;
    CultureInfo culture = CultureInfo.CurrentCulture;
    DateTimeStyles styles = DateTimeStyles.None;
    DateTime.TryParse(datetimestring,culture, styles, out dateValue);
    

    【讨论】:

    • 你救了我的一天,到处寻找并尝试确定如何在变量级别上设置 CUltureInfo(不改变 CurrentCulture),但在 Date DataType 的属性、方法等下找不到它... .TryParse 在典型的 Date 数据类型扩展方法下不可用。当您向我展示 DateTime.TryParse 时,我意识到它超出了 Date 和 DateTime 的可用扩展方法。他们有什么理由这样做吗?因为如果 TryParse 在 Date 类型的扩展方法中可用,您可以声明一个空日期并使用 dateString + CultureInfo 设置自己!???
    • 您应该在 DateTime Conversion 中使用 TryParse 方法,以便安全轻松地进行转换。
    【解决方案5】:
    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;
    
        }
    

    【讨论】:

    • 请编辑您的答案并格式化代码以使其可读
    • @kleopatra 你也可以这样做
    • 也可以考虑添加一些描述。
    • @j0k 我当然可以 - 但我不喜欢一直用勺子喂每个人,他们可以自己学习,不是吗:-)
    • 为什么最后是空捕获?
    【解决方案6】:

    这对我有用,

    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);
    

    【讨论】:

    • 这与当前接受的答案没有什么不同。
    • @helix 我只是交换了文化对象,因为现有答案对我来说会导致错误,我没有足够的点来编辑现有答案。
    • 可能是因为现有答案从美国转换为英国,而您的答案从英国转换为美国。
    • @helix 请在“26/09/2015”和“12/01/2011”这几天检查两个答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-29
    • 1970-01-01
    • 2015-09-17
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多