3

Currently I am trying to get the hour and minute using this code:

DateTime currentDate = DateTime.Now.Date; int currentHour = DateTime.Now.Date.Hour; int currentMin = DateTime.Now.Date.Minute; 

But I am getting zero instead to the current hour and minute. How can I fix this problem?

2
  • 1
    Despite the tags, this is really nothing to do with the C# language at all. It's just .NET. (Fixed now.) Commented Mar 9, 2012 at 7:23
  • 1
    I know you already have a correct answer, but just to know your problem it was the "DateTime.Now.Date" you should have used "DateTime.Now" Commented Mar 9, 2012 at 10:25

5 Answers 5

18

You're using the Date property which gives you midnight at the given DateTime. You just want Now:

DateTime now = DateTime.Now; int hour = now.Hour; int minute = now.Minute; DateTime date = now.Date; // If you really need it 

Note that my code only calls DateTime.Now once, instead of once for hours and then once for minutes1. That means if you call it close to (say) 8 A.M., you might get 7:59 or you might get 8:00 - but you won't get either 7:00 or 8:59, which you could get if you had either of these:

// Bad option 1 (could get 7:00) int hour = DateTime.Now.Hour; // Imagine the clock rolls over now... int minute = DateTime.Now.Minute; // Bad option 1 (could get 8:59) int minute = DateTime.Now.Minute; // Imagine the clock rolls over now... int hour = DateTime.Now.Hour; 

You should also consider not using DateTime.Now directly at all. It has two problems:

  • It returns the time in the local time zone; that's probably okay for desktop applications, but bad for server-side applications
  • It's hard to test

If you create an IClock interface or something similar which has a method or property to give you the current UTC time, you can always convert that to local time if you want (unambiguously, which isn't true in reverse) and you can also inject a fake implementation for test purposes.

EDIT: A short but complete program to prove that yes, this code really does work:

using System; public class Test { static void Main(string[] args) { DateTime now = DateTime.Now; int hour = now.Hour; int minute = now.Minute; Console.WriteLine(hour); Console.WriteLine(minute); } } 

On my machine right now, that prints 7 and then 35. If it prints 0 and then 0, then it must be midnight on your machine.


1 As you may notice, I've added a comment to all the answers which continue to do this. Apologies if this looks like spamming, but I wanted to get the attention of those answerers - it's a common error, and one which I'd like to help eradicate. It's possible that without the comments, posters would have just moved on without ever looking again...

Sign up to request clarification or add additional context in comments.

11 Comments

@rizwanShahid it does. I just checked with my compiler.
@rizwanShahid: I'm giving the right answer. Really, I am. And you claiming it doesn't work without giving any indication of what you're seeing when you try my code doesn't help. (I've just double-checked it myself, and it gave me 7 for the hour, and 27 for the minute... both of which are correct for my local time. What did you see when you tried it?)
@rizwanShahid: That's simply wrong. Perhaps you printed currentHour and currentMinute, which were still using your old code. I'll include a short but complete example, to prove it works. Really, I know what I'm talking about here. Note that the answer you do like is flawed, for reasons I've explained in my answer.
@rizwanShahid: Actually, Jon's solution is more correct than mine, since it takes care of "glitches" that might occur it the time goes from XX:59 to XX+1:00 between the call to get the hour and get the minute, and it most definitly works.
@rizwanShahid Because he is Jon Skeet.
|
2

The DateTime.Date property returns

A new object with the same date as this instance, and the time value set to 12:00:00 midnight (00:00:00).

So it is totally expected that you get all zeros for the "time" fields.

Simply don't use the Date property, but invoke Hour and Minute directly on DateTime.Now.

Note that to get consistent values (and for performance reasons), you sould store the current DateTime.Now result and work on that:

DateTime now = DateTime.Now; DateTime currentDate = now.Date; int currentHour = now.Hour; int currentMinute = now.Minute; 

Finally, note that as long as you don't "look into" the time fraction, there is no point using DateTime.Date at all, simply use the DateTime.Now result.

Comments

1

That is because Date removed the time part of the DateTime, so Date.Hour and Date.Minute will always be 00:00. Try this instead:

DateTime currentDate = DateTime.Now.Date; int currentHour = DateTime.Now.Hour; int currentMin = DateTime.Now.Minute; 

EDIT According to Jon Skeet's comment, I agree completely. I just did the smallest change possible that removed the issue that was giving him the problem, notably the call to .Date.

In a real situation you should use DateTime.Now once, first to get the time, and then extract the parts you need to make sure that you extract parts of the exact same time, and that is has not changed in between. I'm not going to write that code here, since it would be a copy of Jon's answer, so look at his answer for details.

4 Comments

It's a very bad idea to use DateTime.Now repeatedly, rather than evaluating it once. See my answer for details.
I think it would be better to have that code here since this would feature as the accepted answer, we need to make the answers better for others to view/see.
@rizwan Shahid: Since the most correct, and best, answer here belongs to Jon Skeet, I would urge you to mark that as accepted instead. If you won't listen to reason, at least I hope other people reading this question will treat Jon's answer as the real accepted answer here
@V4Vendetta - Let's see if OP makes the correct call first. If not, I'll fix up my answer to reflect those changes.
0

The Date property not contains any information about the hours, minutes, seconds, milliseconds. Use simply DateTime.Now.

Comments

0

Should be:

DateTime currentDate = DateTime.Now.Date; int currentHour = DateTime.Now.Hour; int currentMin = DateTime.Now.Minute; 

2 Comments

Please read my answer for why this code is a bad idea - think about what happens if you start executing this just before the start of an hour...
Thanks @Jon :) Your answer makes a lot of sense.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.