How do I get all dates of a given month in a flutter list?
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:
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]
houba
Updated on December 22, 2022Comments
-
houba over 1 year
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
-
houba over 3 yearsThank you so much. Your last sentence hit the nail on the head: Happy Learning. I'm not at your level, and I'm not sure I totally understand your code.. but I will play with it
-
Alok over 3 yearsHahaha! Hey @houba, don't ever think about levels and all. Every body work at their own pace, and reaches to their pinnacle. I am no different, just a learner, who is learning by this too. Play with it. You will be there too. If you find this useful, I would be happy to see it upvoted and marked as correct. Happy learning once again and All the very best :) Also, look at the newly added section in my answer that is IMPROVEMENT. It would be useful for you :)
-
houba over 3 yearsI got the solution playing around with vars. But I give this 100% points because a) taught me how to think, b) date_utils and dart 2.8.X are incompatible
-
houba over 3 yearsThank you so much. This would have been perfect, but it didn't work for me as apparently this pub.dev is only valid for dart < 2.8.x
-
Alok over 3 yearsThanks @houba, I am really happy to see you learn! All the best. :) Even I learnt about this incompatibility via you, thank you very much.
-
MohammedAli about 2 yearsHey @Alok, how to get date with weekdays also