How to format DateTime in Flutter? Remove milliseconds in DateTime?

2,189

Solution 1

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

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

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

// or you can also use default formats

this.date1 = Jiffy(date).yMMMMd; // March 24, 2021

Solution 2

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

Solution 3

You will need the package intl

final now = DateTime.now();
String dateFormatted = DateFormat('yyyy-MM-dd').format(now);
Share:
2,189
Soner Karaevli
Author by

Soner Karaevli

just coding flutter

Updated on December 27, 2022

Comments

  • Soner Karaevli
    Soner Karaevli over 1 year

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