将C#dateTime格式化为本地字符串格式 [英] Format c# dateTime to local string format
问题描述
我有一个DateTime C#对象(在Unity脚本中,以Android设备为目标).我想通过.toString()(因此没有任何特殊的格式设置参数)检索字符串表示形式,该字符串表示形式应为"10/5/2014 9.17 PM".如果我现在在将语言环境设置为例如的Android设备上运行它德语,只是时间会正确转换,而日期则保持美国格式(因此,"10/5/2014 21.17"而不是"5.10.2014 21.17").我是在这里做错什么,还是我根本不了解整个事情;-)
I have a DateTime C# object (in a Unity script, targeted for an Android device). I want to retrieve a string representation via .toString() (so w/o any special formatting parameter), which is supposed to deliver a short form like "10/5/2014 9.17 PM". If I now run it on an Android device with the locale set to e.g. German, just the time will be converted correctly, while the date stays in US format (so "10/5/2014 21.17" instead of "5.10.2014 21.17"). Am I doing something wrong here, or maybe don't I understand the whole thing at all ;-)
推荐答案
您对 DateTime.ToString
的假设是正确的.以下代码打印05.10.2014 13:26:36:
Your assumption about DateTime.ToString
is correct. The following code prints 05.10.2014 13:26:36:
Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
DateTime time = DateTime.Now;
Console.WriteLine(time.ToString());
我将检查您的环境中 Thread.CurrentThread.CurrentCulture.ToString()
的值是什么,并确保Unity正确设置了它.如果不是这样,您将必须自己做.
I'd check what the value of Thread.CurrentThread.CurrentCulture.ToString()
is in your environment and ensure that Unity sets it correctly. If it doesn't, you would have to do it yourself.
这篇关于将C#dateTime格式化为本地字符串格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!