when user using 24 hours format, I have pick time with showTimepicker have set
alwaysUse24HourFormat:false
and when selecting time displays in 12 hour format but
selected timeofday is 24 hours format ,so how change it into 12 hours format?
I tried below code
TextFormField(
key: _timekey,
inputFormatters: [
FilteringTextInputFormatter.deny(
RegExp(Constant.regexToRemoveEmoji)),
],
autovalidateMode:
AutovalidateMode.onUserInteraction,
focusNode: timefocus,
onFieldSubmitted: (_) {
FocusScope.of(context)
.requestFocus(locationfocus);
},
onTap: () async {
DateTime timenow = DateTime.now();
TimeOfDay? time = TimeOfDay(
hour: timenow.hour,
minute: timenow.minute);
time = await showTimePicker(
context: context,
initialTime: time,
builder: (context, child) {
return MediaQuery(
data: MediaQuery.of(context)
.copyWith(
alwaysUse24HourFormat:
false),
child: child!);
});
if (time != null) {
setState(() {
DateTime startdate =
DateFormat("hh:mm").parse(
time!.hour.toString() +
":" +
time.minute.toString());
var dateFormat = DateFormat("h:mm a");
controller.text =
dateFormat.format(startdate);
});}
),
but sometimes not returns selected AM And PM
2
Answers
For Example: