-1

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);
});}
),

enter image description here

but sometimes not returns selected AM And PM

Deva
  • 70
  • 9

1 Answers1

0

Like in the Flutter Documentation, you can see:
DateFormat.jm() -> 5:08 PM this is predefined from flutter and it will deliver you like above.

...
String formattedTime = DateFormat.jm().format(startdate);
controller.text = formattedTime;
...

You can also use something like that, it will also work:

String formattedTime = DateFormat('h:mm a').format(timenow);
/// a is for am/pm and h is for 12 hour (H is for 24 hours)

If you get a wrong time, it should because of the date! Try to give it also the date. I dont know the purpose of your app. If its today, give it also the Date of today.

MrOrhan
  • 877
  • 6
  • 21
  • 1
    Although this code might answer the question, I recommend that you also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel May 30 '23 at 10:01
  • i tried this method but in sometimes not changing correct AM or PM example i selected 12:53 PM and but return 12:53 AM it replicates 12 to 1 between this time – Deva May 31 '23 at 07:16