How to get rid of the 00:00:00.000 so that it's just the date in Flutter

I want to change the pickerDate to a string in the format of dd-MM-yyyy, right now its in the format yyyy-dd-MM 00:00:00.000. How can I change this?

String _selectedDate = DateFormat("d-MM-yyyy").format(DateTime.now());

_getDateFormUser() async {
    DateTime? pickerDate = (await showDatePicker(
        context: context,
        initialDate: DateTime.now(),
        firstDate: DateTime(2015),
        lastDate: DateTime(2121)));

    if (pickerDate != null) {
      setState(() {
        _selectedDate = pickerDate.toString();
      });
    } else {
      //empty
    }
  }

>Solution :

You’ve already created the DateFormatter, you just need to use it instead of .toString().

_selectedDate =  DateFormat("d-MM-yyyy").format(pickerDate);

Leave a Reply