Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
889 views
in Technique[技术] by (71.8m points)

android - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

    ),
  ),
),

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...