13

I want to remove the time format from this date in a flutter, I want to show a date like this 22-10-2019 or 2019-10-22

2019-10-22 00:00:00.000
1

6 Answers 6

36

There is now a DateUtils class that will handle this, using DateUtils.dateOnly :

DateUtils.dateOnly(date);

under the covers it is just using DateTime(date.year, date.month, date.day) but it reads much better.

Note that it automatically sets date as local regardless of the input, there doesn't seem to be a way to force it to UTC :-(

1
  • Saved lot of time!
    – Devplanet
    Aug 25, 2021 at 13:23
15
var dateTime = DateTime.parse("2019-10-22 00:00:00.000");

var formate1 = "${dateTime.day}-${dateTime.month}-${dateTime.year}";

var formate2 = "${dateTime.year}-${dateTime.month}-${dateTime.day}";

print (formate1);
print (formate2);

OutPut will be :-

22-10-2019

2019-10-22
3
  • is the date in format is the same as dateTime ? May 31, 2021 at 20:42
  • Use variable dateTime instead of date. var dateTime = DateTime.parse("2021-09-27 00:00:00.000"); var formate1 = "${dateTime .day}-${dateTime .month}-${dateTime .year}"; var formate2 = "${dateTime .year}-${dateTime .month}-${dateTime .day}"; print (formate1); print (formate2); Sep 27, 2021 at 14:42
  • 1
    Answer updated @KamauMbûgua Sep 28, 2021 at 5:17
11

Please try below code:-

First you can create below method:-

String convertDateTimeDisplay(String date) {
    final DateFormat displayFormater = DateFormat('yyyy-MM-dd HH:mm:ss.SSS');
    final DateFormat serverFormater = DateFormat('dd-MM-yyyy');
    final DateTime displayDate = displayFormater.parse(date);
    final String formatted = serverFormater.format(displayDate);
    return formatted;
  }

Second you can call above method like below code:-

  String yourDate = '2019-10-22 00:00:00.000';
  convertDateTimeDisplay(yourDate);
7

To reformat your date you should use below method.

String convertedDate = new DateFormat("yyyy-MM-dd").format({your date object});

But if you have date string, so first you need to convert it to date object.

var parsedDate = DateTime.parse('2019-10-22 00:00:00.000');

and then,

String convertedDate = new DateFormat("yyyy-MM-dd").format(parsedDate);
3

Add intl dart package into your project.

import 'package:intl/intl.dart';

Text( DateFormat('dd-MM-yyyy').format(DATE) // replace date with your data

https://youtu.be/VPuUoGzSJ5I

1
  • Awesome and simple to implement Nov 2, 2022 at 14:13
2

If anyone needs to get only date from the current date then use the below code

DateTime currentDateTime = DateTime.now(); 
DateTime onlyDate = DateTime(now.year, now.month, now.day);

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.