2

From the user I receive a DateTime variable. I want to get the name of the month that was entered. Is there a way to do so? (Apart from having a bunch of if statements)

Mo711
  • 533
  • 1
  • 6
  • 21
  • Does this answer your question? [Convert number to month name in PHP](https://stackoverflow.com/questions/18467669/convert-number-to-month-name-in-php) – Leandro Jacques May 23 '20 at 14:42
  • Use PHP's datetime format method using those format flags listed here: https://www.php.net/manual/pt_BR/function.date.php – Leandro Jacques May 23 '20 at 14:45

3 Answers3

14

Use the intl package.

DateFormat("MMMM").format(dateTime);

JideGuru
  • 7,102
  • 6
  • 26
  • 48
5

Another way to get name of month:

List months = ['jan','feb','mar','april','may','jun','july','aug','sep','oct','nov','dec'];
var someDateTime = new DateTime.now();
var mon = someDateTime.month;
print(months[mon+1]);

don't forget to import package:

import 'package:intl/intl.dart';
Zahra
  • 2,231
  • 3
  • 21
  • 41
2

Use it:

import 'package:intl/intl.dart';

//--------------------

final DateTime today = DateTime.now();

final DateFormat format1 = DateFormat('MMM');
final DateFormat format2 = DateFormat('MMMM');

print(format1.format(today)); // abbreviated, exp. : Jan, Feb, Mar
print(format2.format(today)); // not abbreviated, exp. : January, February,
Sapto Sutardi
  • 326
  • 4
  • 11