[英]Xamarin.iOS UITextField date format not working
I've a UITextField which when clicked opens a DatePicker. 我有一个UITextField,单击该对象时将打开一个DatePicker。 I want time to be shown in the TextField in hh:mm tt (like 2:10 PM) 我希望时间在hh:mm tt的TextField中显示(例如2:10 PM)
void InitTimePicker()
{
timePicker = new ModalPickerViewController(ModalPickerType.Date, "Select A Time", this)
{
HeaderBackgroundColor = UIColor.Red,
HeaderTextColor = UIColor.White,
TransitioningDelegate = new ModalPickerTransitionDelegate(),
ModalPresentationStyle = UIModalPresentationStyle.Custom
};
timePicker.DatePicker.Mode = UIDatePickerMode.Time;
timePicker.OnModalPickerDismissed += (sim, ea) =>
{
var timeFormatter = new NSDateFormatter()
{
DateFormat = "hh:mm tt"
};
vitalEntryTimeTextField.Text = timeFormatter.ToString(timePicker.DatePicker.Date);
};
vitalEntryTimeTextField.ShouldBeginEditing = (textView) =>
{
textView.ResignFirstResponder();
PresentViewControllerAsync(timePicker, true);
return true;
};
}
the DatePicker shows and everything works. DatePicker显示,一切正常。 but when I select the time in the datepicker and click done, the time shown in the TextField is like 2:10. 但是当我在日期选择器中选择时间并单击完成时,TextField中显示的时间就像2:10。 AM/PM is not showing though I specified 虽然我指定了AM / PM,但未显示
DateFormat = "hh:mm tt"
What am I doing wrong here? 我在这里做错了什么?
Here you are using a NSDateFormatter
which is from the iOS SDK but specifying the DateFormat
using the C# specifiers 在这里,您使用的是来自iOS SDK的NSDateFormatter
,但使用C#指定符指定 DateFormat
AM/PM will not be shown when the iOS device has 24 hour format turned on: 当iOS设备开启24小时格式时,不会显示AM / PM:
Settings -> General -> Date & Time -> 24 Hour 设置->常规->日期和时间-> 24小时
Obviously you cannot guarantee 24 hour time will not be enabled on every device. 显然,您不能保证不会在所有设备上都启用24小时制。
You can specify the locale for the time format. 您可以为时间格式指定语言环境。 Specifically, en_US
uses 12 hour time by default. 具体来说, en_US
默认使用12小时时间。
NSLocale myLocale = new NSLocale("en_US");
var timeFormatter = new NSDateFormatter()
{
Locale = myLocale,
DateFormat = "hh:mm a"
};
This would give you something like 01:15 PM
. 这将为您提供类似于01:15 PM
。
Use HH:mm a
to give you something like 13:15 PM
. 使用HH:mm a
给您类似13:15 PM
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.