Convert DateTime from English to Spanish

时光总嘲笑我的痴心妄想 提交于 2019-12-07 00:59:15

问题


Does anybody know how to convert a DateTime from English to Spanish?

E.g convert:

Monday, January 01, 2011

into

Lunes, Enero 01, 2011 ???

Thanks in advance.


回答1:


Yyou can use CultureInfo to do this, if you set the current culture in the running thread the date will format in the correct culture http://msdn.microsoft.com/en-us/library/5hh873ya.aspx

in vb.net

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

or c#

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



回答2:


You can use the DateTime.ParseExact Method to parse the input into a DateTime value using an English CultureInfo. Then you can use the DateTime.ToString Method with a Spanish CultureInfo to convert the DateTime value to a string.

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"

Consider that a Spanish user might prefer the Spanish standard format over your custom format though:

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



回答3:


Get the DateTime.Now and translate when you need.

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


来源:https://stackoverflow.com/questions/6837000/convert-datetime-from-english-to-spanish

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!