如何使用MonoTouch以短时间格式将DateTime格式化为本地用户区域设置

问题描述:

我尝试了多种方法,这是其中一种:

I've tried multiple approaches, this is one of them:

System.Globalization.DateTimeFormatInfo format = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat;      
return dt.Value.ToString (format.ShortTimePattern);

此问题是.NET 3.5返回。。作为时间分隔符,这很可能是一个错误(因为我的挪威语言环境使用冒号):

The problem with this one is that .NET 3.5 returns a "." as the time seperator which is most likely a bug (since my Norwegian locale uses a colon): .NET (3.5) formats times using dots instead of colons as TimeSeparator for it-IT culture?

我也在这里看过:

但是在MT的NSDateFormatter上似乎没有setTimeStyle吗?

But MT doesn't seem to have setTimeStyle on the NSDateFormatter?

那么,有人有魔术吗?

So, anyone have any magic tricks up their sleeves?

我的目标是输出:

21:00 / 9:00 PM

21:00 / 9:00 PM

10:09 / 10:09 AM

10:09 / 10:09 AM

就像iPhone中的状态栏一样。

like the statusbar in iPhone.

尝试一下:

var formatter = new NSDateFormatter ();
formatter.TimeStyle = NSDateFormatterStyle.Short;
Console.WriteLine (formatter.ToString (DateTime.Now));

也就是说,以点/冒号作为挪威语言环境时间分隔符的错误已在Mono中修复2.12,因此当MonoTouch切换为使用Mono 2.12代替Mono 2.10(希望在明年年初)时,您也可以使用此托管替代方法:

That said, the bug with dot/colon as the time separator for Norwegian locales has been fixed in Mono 2.12, so when MonoTouch switches to use Mono 2.12 instead of Mono 2.10 (hopefully early next year) you will be able to use this managed alternative too:

Console.WriteLine (DateTime.Now.ToString (new CultureInfo ("nb-NO").DateTimeFormat.ShortTimePattern));