Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7655425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:30:46+00:00 2026-05-31T12:30:46+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:30:47+00:00Added an answer on May 31, 2026 at 12:30 pm

    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…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently trying to get the code coverage report for a PHP application
I'm trying to get current hour minutes and seconds. Here is the code I
I'm currently trying to get this image-map to work: <img src=products1.png usemap=products1-map style=margin-bottom: 36px
I'm currently trying to get into the Java EE development with the Spring framework.
I'm currently trying to get a UISlider to work. I'm trying to set the
I'm currently trying to get the output of an executable console-app into an other
I am currently trying to get data out of a plist. It basically looks
I'm currently trying to get a grip on Android development and working my way
i am currently trying to get Input Mode Editor (IME) support working with a
Heya, I'm currently trying to get bltoolkit working in my project. I've added the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.