How to remove time from date flutter

Viewed 20501

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
6 Answers

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 :-(

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

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

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

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

Add "intl" dart package into your project

1

import 'package:intl/intl.dart';

Put your date time at _dateTime

Text( DateFormat('dd-MM-yyyy').format(_dateTime),

https://youtu.be/VPuUoGzSJ5I

Related