Exercise: Flight Duration Tracker
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
GetFlightDurationStringmethod in theFlightTrackerclass within theAviationnamespace.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
DateTimestruct to represent the takeoff and landing times.Use the
-operator to calculate theTimeSpandifference between the twoDateTimeparameters.Use the
.Hoursand.Minutesproperties of theTimeSpanstruct to format the output. Do not use.TotalHoursor.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
DateTimefrom a newerDateTimenatively returns aTimeSpanobject.The
.Hoursproperty extracts the whole hours component of the duration, ignoring any remaining minutes or seconds.The
.Minutesproperty 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
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
GetFlightDurationStringmethod in theFlightTrackerclass within theAviationnamespace.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
DateTimestruct to represent the takeoff and landing times.Use the
-operator to calculate theTimeSpandifference between the twoDateTimeparameters.Use the
.Hoursand.Minutesproperties of theTimeSpanstruct to format the output. Do not use.TotalHoursor.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
DateTimefrom a newerDateTimenatively returns aTimeSpanobject.The
.Hoursproperty extracts the whole hours component of the duration, ignoring any remaining minutes or seconds.The
.Minutesproperty 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.