0

I need to display date like 22 Dec 2021. How could I achieve this? DateFormat.yMMMd().format(date) this gives date like Dec 22, 2021.

M.A.
  • 113
  • 1
  • 10

2 Answers2

0

Yes, first import package:intl/intl.dart then :

 DateTime now = DateTime.now();
 String formatted = DateFormat('dd MMM yyyy').format(now);
 print(formatted); // prints 22 Dec 2021
esentis
  • 4,190
  • 2
  • 12
  • 28
0

You will need to use intl package for this. Date formatting works using skeletons. In this case what we need is dd, MM and yyyy skeleton.

  final DateFormat formatter = DateFormat('dd MMM yyyy');
  print(formatter.format(date));

Let's say you want 22/Dec/2021 for any reason, you can just paas dd/MMM/yyyy to DateFormat and the date will be returned as needed. You can get a list of all skeletons here. The yMMMd function you have used here uses skeletons under the hood. These are just some of the more common ones.

Chinmay Kabi
  • 489
  • 4
  • 9