Mulesoft Developer
Mulesoft Developer

Reputation: 2824

Get date and time in .NET

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?

Upvotes: 3

Views: 10439

Answers (5)

Jon Skeet
Jon Skeet

Reputation: 1500135

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...

Upvotes: 18

Øyvind Bråthen
Øyvind Bråthen

Reputation: 60694

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.

Upvotes: 1

Christian.K
Christian.K

Reputation: 49240

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.

Upvotes: 2

gideon
gideon

Reputation: 19465

Should be:

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

Upvotes: 0

Peter Kiss
Peter Kiss

Reputation: 9319

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

Upvotes: 0

Related Questions