Home > Mobile >  How to get all dates and weekdays in current month in flutter
How to get all dates and weekdays in current month in flutter

Time:03-01

I want to get current months all dates and weekdays like below image

i get all dates with below code

DateTime now = DateTime.now();
late DateTime lastDayOfMonth;
lastDayOfMonth = DateTime(now.year, now.month   1, 0);

  Row(
    children: List.generate(
       lastDayOfMonth.day,
        (index) => Padding(
          padding: const EdgeInsets.only(right: 24.0),
          child: Text(
            "${index   1}",
          ),
        ),
      ),

but i didn't get weekdays according dates

enter image description here

CodePudding user response:

The lastDayOfMonth is a single date, therefore using lastDayOfMonth.weekDay gives a single day based on lastDayOfMonth. We can add duration on lastDayOfMonth and find day name.

 final currentDate =
           lastDayOfMonth.add(Duration(days: index   1));

This will provide update date based on index. I am using intl package or create a map to get the day name.

A useful answer about date format.

 Row(
  children: List.generate(
    lastDayOfMonth.day,
    (index) => Padding(
      padding: const EdgeInsets.only(right: 24.0),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Text(
            "${index   1}",
          ),
          () {
            final currentDate =
                lastDayOfMonth.add(Duration(days: index   1));

            final dateName =
                DateFormat('E').format(currentDate);
            return Text(dateName);
          }()
        ],
      ),
    ),
  ),
),
  • Related