Exercise: Flight Duration Tracker

Calculate and format the duration of a flight using the TimeSpan struct within a utility class.

Problem statement

An aviation application tracks an aircraft that took off at a specific date and time and landed later. You need to implement a utility method that calculates the exact duration of the flight and returns it in a user-friendly format for the digital flight log.

Task requirements

  • Implement the GetFlightDurationString method in the FlightTracker class within the Aviation namespace.

  • Calculate the exact flight duration by subtracting the takeoff time from the landing time.

  • Extract the whole hours and whole minutes from the resulting duration.

  • Return a string stating the flight time explicitly as “X hours and Y minutes”.

Constraints

  • Use the DateTime struct to represent the takeoff and landing times.

  • Use the - operator to calculate the TimeSpan difference between the two DateTime parameters.

  • Use the .Hours and .Minutes properties of the TimeSpan struct to format the output. Do not use .TotalHours or .TotalMinutes.

  • Return the formatted string using modern string interpolation.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • Subtracting an older DateTime from a newer DateTime natively returns a TimeSpan object.

  • The .Hours property extracts the whole hours component of the duration, ignoring any remaining minutes or seconds.

  • The .Minutes property extracts the whole minutes component that did not divide evenly into a full hour.

  • Make sure your method returns the interpolated string instead of printing it directly.

Exercise: Flight Duration Tracker

Calculate and format the duration of a flight using the TimeSpan struct within a utility class.

Problem statement

An aviation application tracks an aircraft that took off at a specific date and time and landed later. You need to implement a utility method that calculates the exact duration of the flight and returns it in a user-friendly format for the digital flight log.

Task requirements

  • Implement the GetFlightDurationString method in the FlightTracker class within the Aviation namespace.

  • Calculate the exact flight duration by subtracting the takeoff time from the landing time.

  • Extract the whole hours and whole minutes from the resulting duration.

  • Return a string stating the flight time explicitly as “X hours and Y minutes”.

Constraints

  • Use the DateTime struct to represent the takeoff and landing times.

  • Use the - operator to calculate the TimeSpan difference between the two DateTime parameters.

  • Use the .Hours and .Minutes properties of the TimeSpan struct to format the output. Do not use .TotalHours or .TotalMinutes.

  • Return the formatted string using modern string interpolation.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • Subtracting an older DateTime from a newer DateTime natively returns a TimeSpan object.

  • The .Hours property extracts the whole hours component of the duration, ignoring any remaining minutes or seconds.

  • The .Minutes property extracts the whole minutes component that did not divide evenly into a full hour.

  • Make sure your method returns the interpolated string instead of printing it directly.

C# 14.0
namespace Aviation;
public class FlightTracker
{
public static string GetFlightDurationString(DateTime takeoff, DateTime landing)
{
// 1. Calculate the flight duration by subtracting the takeoff time from the landing time
// 2. Return the formatted message using string interpolation and the TimeSpan properties
return "";
}
}