简体   繁体   English

日期和时间格式在iOS上显示不正确

[英]Date and time format shows incorrectly on iOS

I have some trouble with the date format on iOS. 我在iOS上的日期格式遇到一些麻烦。 On Android it displays correctly. 在Android上,它可以正确显示。

var d = local.ToLongDateString();
var t = local.ToShortTimeString();
return string.Format("{0} kl. {1}", d, t);

This will display the date in English, and the time with 12-hour format. 这将以英语显示日期,并以12小时格式显示时间。 The phone region is set to Norway, phone language is Norwegian while English is given as the secondary language. 电话区域设置为挪威,电话语言为挪威语,而英语为第二语言。

Result on iOS: 在iOS上的结果:

Sunday, April 8, 2018 kl. 2018年4月8日星期日 6.00 PM 下午6.00

Result on Android: 在Android上的结果:

søndag 8. april, 2018 kl. søndag8. 2018年4月 18:00 18:00

I found the answer within the Xamarin Docs - iOS Localization : 我在Xamarin文档-iOS本地化中找到了答案:

If CFBundleDevelopmentRegion has not been specified in the Info.plist file, the Xamarin.iOS build tools currently use a default value of en_US. 如果未在Info.plist文件中指定CFBundleDevelopmentRegion ,则Xamarin.iOS生成工具当前使用默认值en_US.

The solution was to edit Info.plist with a text editor and add the following key: 解决方案是使用文本编辑器编辑Info.plist并添加以下键:

<key>CFBundleDevelopmentRegion</key>
<string>no</string>

You could provide a format yourself: 您可以自己提供一种格式:

local.ToString("dddd, MMMM d, yyyy kl. hh:mm tt", CultureInfo.InvariantCulture)

The code above should return a consistent result on all the targeting platforms: 上面的代码应在所有定位平台上返回一致的结果:
Wednesday, April 4, 2018 kl. 10:24 AM

You can easily play with the solution here: http://rextester.com/PPP50237 您可以在此处轻松使用该解决方案: http : //rextester.com/PPP50237

More information on Custom Date and Time Format Strings can be found here . 有关自定义日期和时间格式字符串的更多信息,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM