Soner Karaevli
Soner Karaevli

Reputation: 209

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

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

Upvotes: 11

Views: 15374

Answers (4)

Jama Mohamed
Jama Mohamed

Reputation: 3405

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

Upvotes: 2

Meet Shah
Meet Shah

Reputation: 82

Here in place of "MillisecondsSinceEpoch" put your value

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

Upvotes: 0

CharlyKeleb
CharlyKeleb

Reputation: 754

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

Upvotes: 10

Guillaume Roux
Guillaume Roux

Reputation: 7318

You will need the package intl

final now = DateTime.now();
String dateFormatted = DateFormat('yyyy-MM-dd').format(now);

Upvotes: 2

Related Questions