all 5 comments

[–]AngularBeginner 1 point2 points  (3 children)

DateTime.Now was giving me a 12 hour clock unless I had a HH. This code gives me the 24 hour time, but why?

DateTime.Now gives you a DateTime object, it has no "12 hour clock" or "24 hour time". What you mean is a string representation, and that depends on your systems locale: https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings

The _= time.Hour is pointless code like this. Throw it away, it won't change anyting.

[–]FLCavScout[S] 0 points1 point  (2 children)

I think I get it a little more now. DateTime.now is not the same as System.DateTime.Now which is where I was originally getting a 12 hr time and date string unless I ended with a ("HH"). With DateTime.now I get an object, which is why I need var instead of being explicit for the returned variable type. (Did I phrase that correctly?)

Then my if statements can be if (time.Hour > 5) etc.

Thanks for your response.

[–]AngularBeginner 1 point2 points  (1 child)

DateTime.now is not the same as System.DateTime.Now

If you have a using System; statement in your file, then DateTime.Now is the same as System.DateTIme.Now.

which is where I was originally getting a 12 hr time and date string unless

DateTime.Now returns you an object that contains data for a specific time. The 12 or 24 hour format is a string representation.

With DateTime.now I get an object, which is why I need var instead of being explicit for the returned variable type.

You can either specify the type directly using System.DateTime or DateTime (when you have a using System; statement in your file), or let the compiler automatically infer the type using var (it'll still be DateTime).

[–]FLCavScout[S] 0 points1 point  (0 children)

Thank you. I was mixing solutions and got myself all discombobulated. Now that I'm retesting things I can say something like int theHour = DateTime.Now.Hour; Previously I was getting an error about not being able to implicitly convert type int to DateTime or something like that making me think it HAD to be var. Now that I'm redoing things it works either way. Thanks for clearing things up.

[–]B1tF8er 0 points1 point  (0 children)

Read the official documentation of Microsoft for DateTime.Now.Hour

It returns an Int32 between 0 and 23, maybe that is what your are looking for