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.
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());
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"
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