Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 642
NilxSingh Avatar asked Apr 16 '19 06:04

NilxSingh


People also ask

How do I get only date from timestamp in Flutter?

How do you get only date not time in Flutter? static final DateTime now = DateTime. now(); static final DateFormat formatter = DateFormat('yyyy-MM-dd');

How do I remove time from date in Flutter?

There is now a DateUtils class that will handle this, using DateUtils. dateOnly : DateUtils. dateOnly(date);

How do you enable or disable the past dates in the Flutter date range picker?

Enable and disable past dates The DateRangePicker allows you to enable or disable the past dates from today's date in MonthView . This can be achieved by changing the enablePastDates property. By default, the value of this property is set to true.


1 Answers

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

    ),
  ),
),
like image 108
Neha Bhardwaj Avatar answered Oct 19 '22 17:10

Neha Bhardwaj