【问题标题】:Convert DateTime from English to Spanish将 DateTime 从英语转换为西班牙语
【发布时间】:2011-10-13 19:11:48
【问题描述】:

有人知道如何将DateTime 从英语转换为西班牙语吗?

例如转换:

2011 年 1 月 1 日,星期一

进入

Lunes, Enero 01, 2011 ???

提前致谢。

【问题讨论】:

  • 2011 年 1 月 1 日是星期六。您可能无法正确解析此日期。
  • 西班牙语中的月份没有大写字母。

标签: c# .net datetime datetime-format


【解决方案1】:

您可以使用DateTime.ParseExact Method 将输入解析为使用英文CultureInfoDateTime 值。然后您可以使用DateTime.ToString Method 和西班牙语CultureInfoDateTime 值转换为字符串。

var input = "Tuesday, July 26, 2011";
var format = "dddd, MMMM dd, yyyy";

var dt = DateTime.ParseExact(input, format, new CultureInfo("en-US"));

var result = dt.ToString(format, new CultureInfo("es-ES"));
// result == "martes, julio 26, 2011"

考虑到西班牙用户可能更喜欢西班牙标准格式而不是您的自定义格式:

var result = dt.ToString("D", new CultureInfo("es-ES"));
// result == "martes, 26 de julio de 2011"

【讨论】:

  • +1 表示 "dddd, MMMM dd, yyyy" 格式在西班牙语中是非标准格式。
  • @Mechanicalsnail yes 是标准的:[day] de [month without capital] del [year in 4 digit number]。正确的方式是“del”,但也可以接受“de”。 (我是西班牙大学老师)
【解决方案2】:

您可以使用 CultureInfo 来执行此操作,如果您在正在运行的线程中设置当前文化,则日期将以正确的文化格式化 http://msdn.microsoft.com/en-us/library/5hh873ya.aspx

在 vb.net 中

    Dim TheDate As DateTime = DateTime.Parse("January 01 2011")
Thread.CurrentThread.CurrentCulture = New CultureInfo("es-ES")
MsgBox(TheDate.ToLongDateString)

或c#

DateTime TheDate = DateTime.Parse("January 01 2011");
Thread.CurrentThread.CurrentCulture = new CultureInfo("es-ES");
Interaction.MsgBox(TheDate.ToLongDateString());

【讨论】:

  • 对于那些在波多黎各的人,请记住 ES 来自西班牙。尽管我们使用西班牙语格式的日期,但我们使用句点 (.) 表示十进制数(与西班牙和其他欧洲国家相反)。所以在这种情况下,使用 Thread.CurrentThread.CurrentCulture = new CultureInfo("es-PR");如果您要在正在运行的线程中设置当前文化。
【解决方案3】:

获取 DateTime.Now 并在需要时进行翻译。

private DateTime lastConnection = DateTime.Now;
String dateString =lastConnection.ToString("dd") +" de "+ lastConnection.ToString("MMMM",new CultureInfo("es-ES"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    • 2020-07-12
    • 1970-01-01
    • 1970-01-01
    • 2017-10-26
    相关资源
    最近更新 更多