Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have been given the date and time of an appointment and I need to return how many days it (the appointment) is in the future. The answer must be in FULL days; for example, at 11 PM tonight, an appointment at 9 AM tomorrow would be 1 day (and not 10 hours).

I have attempted the question (please see the code below). However, I get a red squiggly line underneath everything, stating 'cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)'.

Please bear in mind that I am constantly doing other research to learn C#, and lazy is not what I am being by posting questions which I have attempted.

I appreciate your help.

Regards.

What I have tried:

C#
public static int HowManyDaysFromToday(DateTime appointment)
{
  return appointment.TimeOfDay.Subtract(DateTime.Now.TimeOfDay).TotalDays;
}
Posted
Updated 13-Jul-17 4:16am
v2

Well I'd suggest stripping the time out and compare the two dates that way:

C#
public static int HowManyDaysFromToday(DateTime appointment)
{
  var today = DateTime.Today; //like DateTime.Now but with no time aspect
  var appDay = appointment.Date;
  return appDay.Subtract(today).TotalDays;
}


If you wanted then you could return a timespan with the actual time too:
C#
public static TimeSpan HowManyDaysFromToday(DateTime appointment)
{
  var today = DateTime.Today; //like DateTime.Now but with no time aspect
  var appDay = appointment.Date;
  return appDay.Subtract(today) + appointment.Subtract(appDay); // or whatever
}


That way you have all the data in one standard object:

C#
public override string ToString(){
  var ts = HowManyDaysFromToday(_app);
  return string.Format("{0} days at {1}:{2}",(int)ts.TotalDays,ts.Hours,ts.Minutes);
}


That's not a great way to do it, but it's just an idea ^_^
 
Share this answer
 
Comments
Member 13302374 13-Jul-17 9:15am    
Thank you once again. Your knowledge of C# is inspiring me and you also are explaining things, which helps me to develop understanding and not just syntax.
Andy Lanng 13-Jul-17 9:24am    
Very high praise indeed. I'm glad that you feel more confident in your understanding :)
Maciej Los 13-Jul-17 12:32pm    
5ed!
This is basically the same issue as C# - calculate number of hours away from a deadline[^] except that this time you are looking at Days. Given that you want full or partial days you just need to divide the hours by 24 and add 1 if there is a remainder. Simple maths really.
 
Share this answer
 
Comments
Maciej Los 13-Jul-17 12:32pm    
5ed!
Andy Lanng 14-Jul-17 4:10am    
This mostly came up in the chat on the last question (your link): Basically if the dates cross midnight then that is a whole day, so 11pm - 9am is 1 day but 11am - 9pm isn't

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900