How do I get the name of a month

Viewed 2939

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)

3 Answers

Use the intl package.

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

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

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,
Related