0

I am creating a form in flutter where one of the fields is a date of birth. I have used a DateTime picker to pick the date and show the output. I would like to format the date I get to only show month, date,and year. Here is the date format currently YYYY-MM-JJ HH-MM:00.000. I would like it to be in this format MM-dd-yyy. This is the code I came up with

---
DateTime _dateTime;
---
Text(
              _dateTime == null
                  ? 'Nothing has been picked yet'
                  : _dateTime.toString(),
            ),
            ElevatedButton(
              onPressed: () {
                showDatePicker(
                  context: context,
                  initialDate: _dateTime == null ? DateTime.now() : _dateTime,
                  firstDate: DateTime(1930),
                  lastDate: DateTime.now(),
                ).then((date) {
                  setState(() {
                    _dateTime = date;
                    
                  });
                });
              },
              child: Text('Pick a date'),
            )
Kevnlan
  • 487
  • 2
  • 10
  • 31
  • Use [DateFormat](https://api.flutter.dev/flutter/intl/DateFormat-class.html) from [intl](https://pub.dev/packages/intl) package. – rickimaru Apr 22 '21 at 06:50

2 Answers2

1

You can use date format from flutter

final f = DateFormat('yyyy-MM-dd hh:mm');
f.format(date);

You can replace yyyy-MM... To whatever you like

Karim
  • 962
  • 7
  • 10
0

Add the package intl

dependencies:
  intl: ^0.17.0

And call the class DateFormate()

DateFormat("MM-dd-yyyy").format(_dateTime);
Raine Dale Holgado
  • 2,932
  • 2
  • 17
  • 32