Home > Software design >  How can I get all days (name, number) of 4 weeks from current day (in the future)?
How can I get all days (name, number) of 4 weeks from current day (in the future)?

Time:06-23

I want to get all days 4 weeks from my current day. For example, today is June (Thu, 23), I want to get (24, Fri) .... up until (21, Thu) July.

CodePudding user response:

You can generate days like

  late List<DateTime> days = List.generate(
    7 * 4, // 4weeks
    (index) => DateTime.now().add(
      Duration(days: index 1),
    ),
  );

And to get days name I am using intl package. and formatter is

final formatter = DateFormat('EEEE');
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView.builder(
        itemCount: days.length,
        itemBuilder: (context, index) => Text(
            "name: ${formatter.format(days[index])} number:${days[index].day}  "),
      ),
    );
  }

CodePudding user response:

Using DateFormat

final DateFormat formatter = DateFormat('MMMMd');
    _nextDateFormatted = formatter.format(widget.nextDate);

In your case, you will have 4 dates. Heres with the first date

_firstDateFormatted = formatter.format(DateTime.now().add(days:1).millisecondsSinceEpoch)

CodePudding user response:

List<dynamic> data = [];
                        for (int i = 1; i < 29; i  ) {
                          data.add(DateFormatters()
                              .dMy
                              .format(DateTime.now().add(Duration(days: i))));
                        }
                        log("$data");
  • Related