问题
I'd like to know how I could get all dates of a given month and year added into a Dynamic List in flutter?
The idea is to display this information in a data table.
How can I put all dates (for a given month) in a List?
List myListOfDates = Dates(may2020).format(day.month.year)
How can I produce this?
Thanks
回答1:
If I understand your situation right, what I have stipulated is to find all the dates of the particular specified month.
ALGORITHM
- Take the input in a month number and year, and pass it inside DateTime()
- Find the total days of the month provided
- Generate all the dates till the total days we got form the STEP 1
- print to check
Before we proceed to the code, you might want to check this out, this will help you understand the situation better and clear:
- DateTime class
- List.generate
CODE: It doesn't require any imports, so follow along.
void main() {
// Take the input year, month number, and pass it inside DateTime()
var now = DateTime(2020, 7);
// Getting the total number of days of the month
var totalDays = daysInMonth(now);
// Stroing all the dates till the last date
// since we have found the last date using generate
var listOfDates = new List<int>.generate(totalDays, (i) => i + 1);
print(listOfDates);
}
// this returns the last date of the month using DateTime
int daysInMonth(DateTime date){
var firstDayThisMonth = new DateTime(date.year, date.month, date.day);
var firstDayNextMonth = new DateTime(firstDayThisMonth.year, firstDayThisMonth.month + 1, firstDayThisMonth.day);
return firstDayNextMonth.difference(firstDayThisMonth).inDays;
}
OUTPUT
// since we used month 7, in the DateTime(), so it returned 31, which will give output
// till last date of the specified month
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]
IMPROVEMENT
If you want to store the data in this format, dd/mm/yyyy. We can always modify it. It can be done like this, with a little improvements in the code
// make sure you define you List<String> not List<int> in the previous code
// also, in place of May and 2020, you can add your input params for month and year, make sure to convert the numeric month to word format like 7 => July
// like "${i+1} $month, $year"
// I have used my words only
var listOfDates = new List<String>.generate(lastDateOfMonth, (i) => "${i+1}/July/2020");
print(listOfDates);
You can also, store the data in whatever form you like, I liked date/month/year
OUTPUT
[1/July/2020, 2/July/2020, 3/July/2020, 4/July/2020, 5/July/2020, 6/July/2020, 7/July/2020, 8/July/2020, 9/July/2020, 10/July/2020, 11/July/2020, 12/July/2020, 13/July/2020, 14/July/2020, 15/July/2020, 16/July/2020, 17/July/2020, 18/July/2020, 19/July/2020, 20/July/2020, 21/July/2020, 22/July/2020, 23/July/2020, 24/July/2020, 25/July/2020, 26/July/2020, 27/July/2020, 28/July/2020, 29/July/2020, 30/July/2020, 31/July/2020]
回答2:
First, we will need this date_util package to obtain the number of days in a month.
Second, we will need this intl package to obtain the DateTime formate.
You can try it through the following code:
import 'package:date_util/date_util.dart';
import 'package:intl/intl.dart';
@override
void initState() {
final daysCount = DateUtil().daysInMonth(DateTime.may, 2020);
List<String> days = [];
for (int i = 1; i < daysCount+1; i++) {
days.add(DateFormat(DateFormat.YEAR_MONTH_DAY)
.format(DateTime(2020, DateTime.may, i)));
}
super.initState();
}
you can change DateFormat.YEAR_MONTH_DAY to any thing you need
This is an image of the output
来源:https://stackoverflow.com/questions/63058039/how-do-i-get-all-dates-of-a-given-month-in-a-flutter-list