checkDateBelongsMonth method

Set<int> checkDateBelongsMonth(
  1. String dateString,
  2. dynamic mapData
)

Implementation

Set<int> checkDateBelongsMonth(String dateString, mapData) {
  // Define the given date
  DateTime givenDate =
      getDateStringCon(dateString); // Example date: January 2, 2024

  // Get the current date
  DateTime currentDate = DateTime.now();

  // Extract the month and year components from the given date
  int givenMonth = givenDate.month;
  int givenYear = givenDate.year;

  // Extract the month and year components from the current date
  int currentMonth = currentDate.month;
  int currentYear = currentDate.year;

  // Calculate the previous month and the month before it
  int previousMonth = currentMonth - 1 <= 0 ? 12 : currentMonth - 1;
  int previousMonthYear =
      currentMonth - 1 <= 0 ? currentYear - 1 : currentYear;

  int twoMonthsAgo = previousMonth - 1 <= 0 ? 12 : previousMonth - 1;
  int twoMonthsAgoYear =
      previousMonth - 1 <= 0 ? previousMonthYear - 1 : previousMonthYear;

  // Compare the month and year components
  if (givenYear == currentYear && givenMonth == currentMonth) {
    mapData.add(1);
    debugPrint('The given date belongs to the current month.');
  } else if ((givenYear == currentYear && givenMonth == previousMonth)) {
    mapData.add(2);
    debugPrint(
        'The given date belongs to the previous month or two months ago.');
  }
  else if ((givenYear == twoMonthsAgoYear && givenMonth == twoMonthsAgo)) {
    mapData.add(3);
    debugPrint(
        'The given date belongs to the previous month or two months ago.');
  } else {
    debugPrint(
        'The given date is not in the current, previous, or next month.');
  }

  return mapData;
}