how to remove time stamp or get only date form datepicker in flutter default datepicker?

前提是你 提交于 2020-01-05 05:35:07

问题


I am trying to get date from default datepicker widget of flutter but when i select date i get time with it. I only want date not time. & would also like to change date format if possible.

This is what i have used from example somewhere.

DateTime selectedDate = DateTime.now();

Future<Null> _selectDate(BuildContext context) async {
    final DateTime picked = await showDatePicker(
        context: context,
        initialDate: selectedDate,
        firstDate: DateTime(2019),
        lastDate: DateTime(2020));
    if (picked != null && picked != selectedDate)
      setState(() {
        selectedDate = picked;
      });
  }

This is where I am getting value to string. It prints like "2019-04-16 12:18:06.018950"

child: FlatButton(
                        materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                        onPressed: () {
                          _selectDate(context);
                          print("Selected Date = $selectedDate");
                        },
                        child: Text(selectedDate == null
                            ? "Select Date"
                            : selectedDate.toString()),
                      )),

I want only date from this "2019-04-16 12:18:06.018950" and also change the format of date displaying like "dd-mm-yyyy"

is this possible?


回答1:


I make the Custom DateFormat you can use this code then you will get the Output as : 16-04-2019

TextEditingController _datecontroller = new TextEditingController();

        var myFormat = DateFormat('d-MM-yyyy');

        Future<void> _selectDate(BuildContext context) async {
            final DateTime picked = await showDatePicker(
                context: context,
                initialDate: date,
                firstDate: DateTime(2015, 8),
                lastDate: DateTime(2101));
            setState(() {
              date = picked ?? date;
            });
          }

        InkWell(
                            onTap: () => _selectDate(context),
                            child: IgnorePointer(
                              child: TextField(
                                controller: _datecontroller,
                                decoration: InputDecoration(

                                  hintText: ('${myFormat.format(date)}'),
                                ),

                              ),
                            ),
                          ),



回答2:


You can use the DateTime class for that. Simply parse your String and access individual parts as a property.

var date = DateTime.parse("2019-04-16 12:18:06.018950");
var formattedDate = "${date.day}-${date.month}-${date.year}";

print (formattedDate);

This will output

16-4-2019

Here's the official documentation.



来源:https://stackoverflow.com/questions/55702224/how-to-remove-time-stamp-or-get-only-date-form-datepicker-in-flutter-default-dat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!