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'),
)