9

As you seen, i have a custom DatePicker widget and it takes the currentTime in DateTime type.

 DatePickerWidget(
                    currentTime: DateTime.now(),
                    text: "$date1",
                    onChanged: (date) {
                      setState(() {
                        getProductDate(date.toString());
                        this.date1 = date.toString();
                      });
                    },
                    onConfirm: (date) {
                      setState(() {
                        getProductDate(date.toString());

                        this.date1 = date.toString();
                      });
                    },
                  ),

but it's give me milliseconds too.

result

Result

YYYY-MM-JJ HH-MM:00.000

How can I remove the :00.000 part in DateTime type?

I just want to this format: 'yyyy-MM-dd'.

But currentTime is getting only DateTime type.

is there any idea?

my DatePickerWidget code:

class DatePickerWidget extends StatelessWidget {
  final Function(DateTime data) onChanged;
  final Function(DateTime data) onConfirm;
  final String text;
  final DateTime currentTime;

 
 

  const DatePickerWidget(
      {Key key, this.onChanged, this.onConfirm, this.text, this.currentTime})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ButtonWidget(
      onPressed: () {
        DatePicker.showDatePicker(context,
            theme: DatePickerTheme(
              containerHeight: context.dynamicHeight(0.3),
            ),
            showTitleActions: true,
            minTime: DateTime(2021, 1, 1),
            maxTime: DateTime(2028, 12, 29),
            onChanged: onChanged,
            onConfirm: onConfirm,
            currentTime: currentTime,
            locale: LocaleType.tr);
      },
      text: text,
      buttonColor: Colors.white,
      borderColor: Colors.black,
      textColor: Colors.black,
    );
  }
}
Soner Karaevli
  • 189
  • 1
  • 2
  • 10
  • Does this answer your question? [How to format DateTime in Flutter , How to get current time in flutter?](https://stackoverflow.com/questions/51579546/how-to-format-datetime-in-flutter-how-to-get-current-time-in-flutter) – nvoigt Feb 24 '21 at 11:21
  • no, i already checked that too. My problem is different :/ – Soner Karaevli Feb 24 '21 at 11:22
  • Different how? It's an *exact* duplicate. Why is your problem different? – nvoigt Feb 24 '21 at 11:23
  • sir, my currentTime is getting only "DateTime" type. I checked all solutions from your questions and doesn't work on me :( – Soner Karaevli Feb 24 '21 at 11:27
  • which code is displaying the date? I mean from your image. – John Joe Feb 24 '21 at 11:34
  • the text: "$date1", is displaying the date. If user select the date, i am going to display the selected date in button's text. – Soner Karaevli Feb 24 '21 at 11:38
  • And why is none of those answers helping you? Any single one is producing the output you want. Maybe I did not understand your question correctly? – nvoigt Feb 24 '21 at 11:39

4 Answers4

9

You can use DateFormat from intl package.

import 'package:intl/intl.dart';

DateTime now = DateTime.now();
String formattedDate = DateFormat('yyyy-MM-dd – kk:mm').format(now);

You can also do it without adding a dependecy

DateTime.now()
            .toString()
            .substring(0,10)
     );   

0

CharlyKeleb
  • 587
  • 4
  • 17
2

You will need the package intl

final now = DateTime.now();
String dateFormatted = DateFormat('yyyy-MM-dd').format(now);
Guillaume Roux
  • 6,352
  • 1
  • 13
  • 38
2

Try out Jiffy, to make it easier to work with date and time

To format your DateTime just pass in your result date, see below

this.date1 = Jiffy.parseFromDateTime(date).format('yyyy-MM-dd'); // 2021-03-24

// or you can also use default formats

this.date1 = Jiffy.parseFromDateTime(date).yMMMMd; // March 24, 2021
Jama Mohamed
  • 3,057
  • 4
  • 29
  • 43
0

Here in place of "MillisecondsSinceEpoch" put your value

var startDate = DateTime.fromMillisecondsSinceEpoch(int.parse("MillisecondsSinceEpoch"));