93

My Date-Time format at the moment is this

By using this :

Text(new DateTime.fromMillisecondsSinceEpoch(values[index]["start_time"]*1000).toString(), 

I am getting the type of format attached in the picture, however I was wondering if I could get it in dd/MM/YYYY hh:mm??

2
  • 2
    check this link Jun 8, 2018 at 10:06
  • 2
    @PratikPrajapati that documentation is horrible. It is very difficult to find a specific format and most of them don't even have an example of what it produces. They need to take a lesson from PHP on how to document DateTime formatting. Sep 30, 2021 at 3:18

11 Answers 11

130

If you use the intl package

final f = new DateFormat('yyyy-MM-dd hh:mm');

Text(f.format(new DateTime.fromMillisecondsSinceEpoch(values[index]["start_time"]*1000)));
2
65

Use intl package:

import 'package:intl/intl.dart';

The following code converts 31/12/2000 23:59 to 12/31/2000 11:59 PM

var inputFormat = DateFormat('dd/MM/yyyy HH:mm');
var inputDate = inputFormat.parse('31/12/2000 23:59'); // <-- dd/MM 24H format

var outputFormat = DateFormat('MM/dd/yyyy hh:mm a');
var outputDate = outputFormat.format(inputDate);
print(outputDate); // 12/31/2000 11:59 PM <-- MM/dd 12H format
0
27

First install the pub.dev/packages/intl package in your pubspec.yaml

   intl: ^0.16.1

Then use

   final df = new DateFormat('dd-MM-yyyy hh:mm a');
   int myvalue = 1558432747;
   print(df.format(new DateTime.fromMillisecondsSinceEpoch(myvalue*1000)));

Output

21-05-2019 10:59 AM

0
17

You can use date_format plugin available here https://pub.dartlang.org/packages/date_format

Then to convert,

formatDate(DateTime.now(), [dd, '/', mm, '/', yyyy, ' ', HH, ':', nn])
1
  • This package seems lighter and better maintained than the intl package Jan 3, 2020 at 6:05
6
/// Get date as a string for display.
String getFormattedDate(String date) {
  /// Convert into local date format.
  var localDate = DateTime.parse(date).toLocal();

  /// inputFormat - format getting from api or other func.
  /// e.g If 2021-05-27 9:34:12.781341 then format should be yyyy-MM-dd HH:mm
  /// If 27/05/2021 9:34:12.781341 then format should be dd/MM/yyyy HH:mm
  var inputFormat = DateFormat('yyyy-MM-dd HH:mm');
  var inputDate = inputFormat.parse(localDate.toString());

  /// outputFormat - convert into format you want to show.
  var outputFormat = DateFormat('dd/MM/yyyy HH:mm');
  var outputDate = outputFormat.format(inputDate);

  return outputDate.toString();
}
1
  • what if i want to send the "2021-05-27 9:34:12.781341" time format to API. how should I do it?
    – palak
    Aug 26, 2021 at 10:08
5

If you use the intl package:

var date = DateTime.fromMicrosecondsSinceEpoch(miliseconds * 1000)
DateFormat(DateFormat.YEAR_MONTH_DAY, 'pt_Br').format(date.toUtc())

Output: 10 de abril de 2020

3

try this

Method:

    getFormatedDate(_date) {
      var inputFormat = DateFormat('yyyy-MM-dd HH:mm');
      var inputDate = inputFormat.parse(_date);
      var outputFormat = DateFormat('dd/MM/yyyy');
    return outputFormat.format(inputDate);
    }

Call:

getFormatedDate(_start_date)// simple date in string format

output:

24/05/2021
3
  • DateFormat is not accessible. i think you need to add the dependencies for this library. Mar 10, 2022 at 14:11
  • add the intl package May 26, 2022 at 12:28
  • sure next time i'm try it. Jun 27, 2022 at 13:12
2
var dateInFormatText = widget.snapshot["date"].toString().split("/");

DateTime dateResult = new DateTime.utc(
int.parse(dateInFormatText[2]),     
int.parse(dateInFormatText[1]), 
int.parse(dateInFormatText[0]));

2
  • 1
    Please add more information to the answer to make it more comprehensive.
    – Parth Shah
    Jul 17, 2020 at 22:18
  • You can just get the string to split and create a datetime object dd/MM/YYYY. Você pode apenas pegar a string fazer um split e criar um objeto datetime com o padrão dd/MM/YYYY. Nov 23, 2020 at 22:34
2

Just need to follow 5 steps:

Step 1: locate pubspec.yaml file and check dependencies of your application. if it doesn't have intl dependency created yet, make sure to add in your pubspec.yaml file.

dependencies:
  flutter:
    sdk: flutter
  intl: ^0.18.0<you can change to desired version>

Step2: Just click CMD+S for MAC OS or Cntrl+S for Windows.

Step 3: observe the bottom toolbar of your editor if you are using VS Code. it will show a message to say it's installing extension

Step 4: Make sure to import the package in your class

import 'package:intl/intl.dart';

Step 5: if you want to display date on Text Widget use below code else just use DateFormatter

Text(DateFormat.yMMMEd().format(item.date)) // this extension gives lots of formats. feel free to use based on your requirement.

Good luck!

1

you can use date_format package to format dates in flutter.

import 'package:date_format/date_format.dart';

final formattedStr = formatDate(DateTime.now(), [dd, '/', mm, '/', yyyy, ' ', HH, ':' nn]);

//02-03-2021
0

If DateFormat() is not working in your case you must add "intl" package by running this command on your terminal:

flutter pub get intl
1
  • 1
    Though we thank you for your answer, it would be better if it provided additional value on top of the other answers. In this case, your answer does not provide additional value, since almost every other answer posted that same solution. If a previous answer was helpful to you, you should vote it up once you have enough reputation
    – lepsch
    Sep 9, 2022 at 13:17

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.