Published: Monday 20 November 2023
This coding challenge will see you write a C# function to work out the number of days between two dates.
Two dates will be passed in as parameters into the function. The later date will subtract the earlier date and from there, we will return the total number of days between the two dates.
Start off with this function:
public static class DateFunctions
{
public static int DaysLeft(DateTime fromDate, DateTime toDate)
{
}
}
Go ahead and return the number of days between theĀ fromTime
and toTime
parameter.
Things to consider:
Subtract
method in DateTime
can be used to subtract two DateTime
objects. This methods returns a TimeSpan
and the TotalDays
property can be used to work out the number of days.DaysLeft
method returns an integer, but the TotalDays
property in TimeSpan
returns a double. Work out how to convert it to an integer.