Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi there,

A question given to me is asking me to return the total minutes passed today until now (i.e. the time right now).

I know the right code format is myDateTime.TimeOfDay.TotalMinutes.
which will give us the number of minutes that have passed in the day represented by myDateTime starting from midnight.

When I tried the code below, I got an error (squiggly red line) saying cannot implicity convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?). What does this mean and can someone correct my code please? The right answer shouldn't be long as it is a simple training course. I know I'm not too far off from the right answer.

Thanks.

What I have tried:

{
        /****************************************************************
        
        Return how many minutes have passed today up to now.
        
        *****************************************************************/
        public static int GetTotalMinutes()
        {
          return DateTime.Now.TimeOfDay.TotalMinutes; 
        }
    }
}
Posted
Updated 11-Jul-17 0:46am
v2

1 solution

public static int GetTotalMinutes()
{
    return (int)DateTime.Now.TimeOfDay.TotalMinutes;
}
 
Share this answer
 
Comments
Member 13302374 11-Jul-17 6:52am    
Thanks! It was correct. Their answer was
return (int) DateTime.Now.TimeOfDay.TotalMinutes.Round(0);

Do you know the significance of the Round(0)?
F-ES Sitecore 11-Jul-17 8:41am    
It'll just explictly control how the decimal is converted into an integer, eg is 1.6 1 as an integer or 2. If you use the cast (int) then the framework makes these decisions.
Richard Deeming 11-Jul-17 11:26am    
Unless they've defined some custom extension methods[^] somewhere, that's going to produce a compiler error. The TotalMinutes[^] property returns a Double[^], which doesn't have a method called Round.
F-ES Sitecore 11-Jul-17 11:43am    
Yeah I know, I CBA going into it lol. I'm not spending my time explaining incredibly basic things to people that they should really know.
Member 13302374 11-Jul-17 11:45am    
Wow, the arrogance. You are good at coding, okay, I get it. And there are things I could teach you which you probably know nothing about. Be humble lol

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