I am trying to get dates from week number in flutter. But I dont know how to do it. I got the week number and the days of the week. Now I want to get the dates from the days in this week.
For example: week 2 Monday date, week 2 Tuesday date, week 2 Friday date
I have started with this code but I need to solve it in another way.
int weeknumber = 2;
//1=monday, 2=tuesday, 5=friday
List<int> weekdays = [1,2,5];
int year = 2023;
int totaldays = weeknumber * 7;
final extraDuration = Duration(days: totaldays);
final startDate = DateTime(year);
final dates = startDate.add(extraDuration);
print(dates);
Here is the output: 2023-01-15 00:00:00.000
But I want: 2023-01-09, 2023-01-10, 2023-01-13
What can I do to get these dates?
CodePudding user response:
If you want this output Monday:2023-01-09 00:00:00.000
Tuesday:2023-01-10 00:00:00.000
Friday:2023-01-13 00:00:00.000
you can work on this code
import 'package:intl/intl.dart';
void main() {
int weeknumber = 2;
//1=monday, 2=tuesday, 5=friday
List<String> weekdays = ['Monday','Tuesday','Friday'];
int year = 2023;
int firstDayOfWeek = (weeknumber-1) * 7;
final extraDuration = Duration(days: firstDayOfWeek);
final startDate = DateTime(year);
final dates = startDate.add(extraDuration);
for (var i = 0; i < 7; i ) {
var newDate = dates.add(Duration(days: i));
if(DateFormat('EEEE').format(newDate) == 'Monday')print('Monday:' newDate.toString());
else if(DateFormat('EEEE').format(newDate) == 'Tuesday')print('Tuesday:' newDate.toString());
else if(DateFormat('EEEE').format(newDate) == 'Friday')print('Friday:' newDate.toString());
}
print(dates);
}
CodePudding user response:
You can use the DateTime constructor to get the date of the first day of the week, and then add an offset to get the other days of the week.
To get the date of the first day of the week, you can use the weekday property of the DateTime object, which gives the day of the week as an integer (Monday is 1, Tuesday is 2, etc.). You can subtract this value from the day of the week you're interested in to get the offset, and then use the add method to get the date of that day of the week.
Here is an example of how you can use this approach to get the dates of Monday, Tuesday, and Friday in week 2 of year 2023:
int weekNumber = 2;
List<int> weekdays = [1, 2, 5];
int year = 2023;
// get the date of the first day of the week
DateTime startDate = DateTime(year, 1, 1);
while (startDate.weekday != DateTime.monday) {
startDate = startDate.add(Duration(days: 1));
}
// add the offset to get the date of the other days of the week
for (int weekday in weekdays) {
int offset = (weekday - 1) (weekNumber - 1) * 7;
DateTime date = startDate.add(Duration(days: offset));
print(date);
}
This will output:
2023-01-09 00:00:00.000
2023-01-10 00:00:00.000
2023-01-13 00:00:00.000
It's important to notice that it's using the DateTime constructor with month=1 and day=1, which is the first day of year, in the first line. Then it's using a while loop to find the first day of the week, which is always the first Monday of the year. After that, it's calculating the offset for each day you want to find, which are weekdays of interest.