- Home
- .NET coding challenges
- Work out the number of days between two dates
Work out the number of days between two dates
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 class DateFunctions
{
public int DaysLeft(DateTime fromDate, DateTime toDate)
{
}
}
Go ahead and return the number of days between the fromTime
and toTime
parameter.
Things to consider:
- The
Subtract
method inDateTime
can be used to subtract twoDateTime
objects. This methods returns aTimeSpan
and theTotalDays
property can be used to work out the number of days. - The
DaysLeft
method returns an integer, but theTotalDays
property inTimeSpan
returns a double. Work out how to convert it to an integer. - The number of days returned should not be lower than 0.
Give us your anonymous feedback regarding this page, or any other areas of the website.
Related challenges
Removing the last character from a string
This coding challenge involves writing a C# method that will removing the last character from a string and returning the value.
Contains online code editor
Write a C# function to convert MPH to KPH
Take our coding challenge to write a C# function with the necessary parameters that converts miles-per-hour (MPH) to kilometres-per-hour (KPH) and vice-versa.
Contains online code editor