Home > Enterprise >  Flutter - date format for print date like MMMdy
Flutter - date format for print date like MMMdy

Time:12-22

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.

CodePudding user response:

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

CodePudding user response:

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.

  • Related