1

How to get dateTime from week number and year in dart Example,

year = 2021 week = 35 return startDate = 29-08-2021 endDate = 04-09-2021

Azhar
  • 11
  • 1
  • Is your question on how to calculate the date, or about the specific dart syntax? – derpirscher Aug 30 '21 at 18:07
  • 1
    Furhtermore you should specify, what the frist day of a week is (seemingly a sunday) and what the first week of the year is. Your current expectations neither fit the european ISO standard (where week 35 would be from 2021-08-30 to 2021-09-05) nor the US standard (where week 35 would be from 2021-08-22 to 2021-08-28) – derpirscher Aug 30 '21 at 18:25
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 03 '21 at 09:53
  • The question is pretty simple, how to calculate (a formula) to get the days interval from a given week of year and a given year. 1st day of week cand be calculated by iso8601, where Monday is considered the 1st Day of week, then the 1st week of year is always 4th of Jan. Maybe also a formula for American calendar. – Xao Apr 04 '23 at 13:31

1 Answers1

0

You may be looking for the ISO 8601 week number (and not one of the other week numbering systems).

If that is the case, week 1 is always the week with January 4th in it, and a week always begins on a Monday and ends on a Sunday. From that you can calculate the Monday for any ISO week number. Below is a compact dart function that does this:

DateTime mondayOfIsoWeek(int isoYear, int isoWeek) {
    // ISO week 1 always has January 4th in it
    final jan4 = DateTime(isoYear, 1, 4);

    // look back to find the Monday of that week
    final mondayBeforeOrOnJan4 = jan4.add(Duration(days: -(jan4.weekday - 1)));

    // add weeks to arrive at the Monday of isoWeek
    return mondayBeforeOrOnJan4.add(Duration(days: 7 * (isoWeek - 1)));
  }
GHH
  • 761
  • 7
  • 13