getMonthName method

String getMonthName({
  1. bool isShort = false,
})

Extension method to get the name of a month from a given number.

Example usage:

1.getMonthName() => "January"
2.getMonthName(true) => "Feb"

@param isShort If true, returns the short form of the month name. Default is false. @return Returns the name of the month as a string. If the input number is not a valid month, returns "Invalid number of month".

Implementation

String getMonthName({bool isShort = false}) {
  final Map<int, String> monthNames = <int, String>{
    1: isShort ? 'Jan' : 'January',
    2: isShort ? 'Feb' : 'February',
    3: isShort ? 'Mar' : 'March',
    4: isShort ? 'Apr' : 'April',
    5: isShort ? 'May' : 'May',
    6: isShort ? 'Jun' : 'June',
    7: isShort ? 'Jul' : 'July',
    8: isShort ? 'Aug' : 'August',
    9: isShort ? 'Sept' : 'September',
    10: isShort ? 'Oct' : 'October',
    11: isShort ? 'Nov' : 'November',
    12: isShort ? 'Dec' : 'December',
  };

  return monthNames[this] ?? 'Invalid number of month';
}