Function to calculate the days passed

#FidelitasUniversity

This function works when a date is selected in a calendar. The date selected is a variable which is used to calculate the days passed in the same year of the variable selected.

Example:

Day selected in the calendar "Wednesday, Feb 01, 2023".

Days passed: 32 days (31 days from January + 1 from February) Calculated within the same year(2023)

Code:

int? daysPassed(DateTime? dateSelected) {
  /// MODIFY CODE ONLY BELOW THIS LINE

  if (dateSelected != null) {
    DateTime startDate = DateTime(dateSelected.year, 1, 0); /// Variable to get the fist date of the year
    Duration timepassed = dateSelected.difference(startDate); /// Variable to get the time passed
    int daysPassed = timepassed.inDays; /// Variable to transform the duration to days passed

    return daysPassed;
  } else {
    return null;
  }

  /// MODIFY CODE ONLY ABOVE THIS LINE
}

7
1 reply