0

So i have a json object which displays year, month and week of year:

{
  "year": 2021,
  "month": 8,
  "week": 31,
},
{
  "year": 2021,
  "month": 8,
  "week": 32,
}

My question is how can i iterate through the json object then convert the week of year into day of month where the day is the end of the week and then pass in the year, month and day of month into a DateTime object in flutter like this:

DateTime(2021,8,7)
DateTime(2021,8,14)

FDjawid
  • 241
  • 3
  • 23

1 Answers1

2

Multiply the week with a 7 to get the total number of days. Like

 int totaldays = week*7;
 final extraDuration = Duration(days: totaldays);
 final startDate = DateTime(2021);

  final newDateTime = startDate.add(extraDuration);

 print(newDateTime);
 print(newDateTime.next(DateTime.friday));
Kaushik Chandru
  • 15,510
  • 2
  • 12
  • 30
  • Thank you this is the answer i was looking for – FDjawid Nov 21 '21 at 21:58
  • This won't necessarily return a day "at the end of the week", whatever the OP means by that. – jamesdlin Nov 21 '21 at 22:20
  • That is true. I just showed a fraction of the json data i am using it is actually all weeks from the year 2021. When i used the example i noticed that after i use week 35 i get 3 september which is not the the end of the first week of that month – FDjawid Nov 21 '21 at 22:40
  • I found the solution but it is in php. I am not sure how i can achieve the same with dart: https://stackoverflow.com/questions/4861384/php-get-start-and-end-date-of-a-week-by-weeknumber – FDjawid Nov 21 '21 at 23:00
  • Just use `print(someDay.next(DateTime.friday))` Edited my answer too. – Kaushik Chandru Nov 22 '21 at 07:06