how to remove time stamp or get only date form datepicker in flutter default datepicker?
Solution 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;
});
}
and Inkwell widget goes like this
InkWell(
onTap: () => _selectDate(context),
child: IgnorePointer(
child: TextField(
controller: _datecontroller,
decoration: InputDecoration(
hintText: ('${myFormat.format(date)}'),
),
),
),
),
Solution 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.
Solution 3
You can also use DateFormat
class from intl package.
import 'package:intl/intl.dart';
DateFormat('dd-MM-yyyy').format(DateTime.now());
More details here.
NilxSingh
Updated on December 10, 2022Comments
-
NilxSingh over 1 year
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?
-
NilxSingh about 5 yearsThank you for the help. It works perfectly... Btw I can do same for the time ri8?
-
NilxSingh about 5 yearsIt worked but still I had to do some work around... Any way it works thanx
-
DroidDev almost 4 yearsIf I select Jan 1st, date.day gives value as '1', How can I get '01'? is there any simple way to get then to check the value and add 0 at the start?