Dart/Flutter – How to find the first date and the last date of a week

In this post, I will share the methods to find the first date and the last date of a week using a provided date.

In below example, I assume Monday is the first day of the week and Sunday is the last day of the week.

1. Find the first date of the week

/// Find the first date of the week which contains the provided date.
DateTime findFirstDateOfTheWeek(DateTime dateTime) {
  return dateTime.subtract(Duration(days: dateTime.weekday - 1));
}

2. Find the last date of the week

/// Find last date of the week which contains provided date.
DateTime findLastDateOfTheWeek(DateTime dateTime) {
  return dateTime.add(Duration(days: DateTime.daysPerWeek - dateTime.weekday));
}

3. Testing

void main() {
  // Find first date and last date of THIS WEEK
  DateTime today = DateTime.now();
  print(findFirstDateOfTheWeek(today));
  print(findLastDateOfTheWeek(today));

  // Find first date and last date of any provided date
  DateTime date = DateTime.parse('2020-11-24');
  print(findFirstDateOfTheWeek(date));
  print(findLastDateOfTheWeek(date));
}

// Output
2020-11-23 06:54:42.865446
2020-11-29 06:54:42.865446
2020-11-23 00:00:00.000
2020-11-29 00:00:00.000

4. Bonus 1: Find first date and last date of previous week

/// Find first date of previous week using a date in current week.
/// [dateTime] A date in current week.
DateTime findFirstDateOfPreviousWeek(DateTime dateTime) {
  final DateTime sameWeekDayOfLastWeek =
      dateTime.subtract(const Duration(days: 7));
  return findFirstDateOfTheWeek(sameWeekDayOfLastWeek);
}

/// Find last date of previous week using a date in current week.
/// [dateTime] A date in current week.
DateTime findLastDateOfPreviousWeek(DateTime dateTime) {
  final DateTime sameWeekDayOfLastWeek =
      dateTime.subtract(const Duration(days: 7));
  return findLastDateOfTheWeek(sameWeekDayOfLastWeek);
}

5. Bonus 2: Find first date and last date of next week

/// Find first date of next week using a date in current week.
/// [dateTime] A date in current week.
DateTime findFirstDateOfNextWeek(DateTime dateTime) {
  final DateTime sameWeekDayOfNextWeek = dateTime.add(const Duration(days: 7));
  return findFirstDateOfTheWeek(sameWeekDayOfNextWeek);
}

/// Find last date of next week using a date in current week.
/// [dateTime] A date in current week.
DateTime findLastDateOfNextWeek(DateTime dateTime) {
  final DateTime sameWeekDayOfNextWeek = dateTime.add(const Duration(days: 7));
  return findLastDateOfTheWeek(sameWeekDayOfNextWeek);
}

Tagged : / / / / /
Subscribe
Notify of
guest

2 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Archana Agarwal
2 years ago

Wow what a mind-blowing piece of blog to address Magento web development queries, you actually sticked to the rla-ones. I can assure that many readers like me must relate with most of the issues. 

In addition, i agree that the developers should work at upgrading themselves and i upgrade with practical execution of skills though a feeling platform Eiliana.com and considerably til now i ‘ m not disappointed.

Al Mamun
2 years ago

Wow

2
0
Would love your thoughts, please comment.x
()
x