C# working with TIME only, and no DATE

Posted on 2009-05-15
Last Modified: 2012-06-21
I've been trying to find a way to determine if my current time of day is between a start and stop time. I don't really care about the date, and actually would prefer to not use it at all.   I've tried a number of approach angles on this, but am not having success.  I'm a novice, so assume nothing on my part when proposing a solution, please.

My current code is:
if (DateTime.Compare(DateTime.Now, dtStart) > 0 && DateTime.Compare(DateTime.Now, dtEnd) < 0)
In this, dtStart = {5/15/2009 7:00:00 AM}  and dtEnd = {5/15/2009 12:30:00 PM}

It's currently 7:39 AM so we are between those times, yet my DateTime.Compare above yields False, so my code bails out on the else of the if/else.  

I'm TOTALLY open to approaching this from a different angle if one is more reliable.  I just hate working with the DateTime object.  It is exceedingly confusing to me.
Question by:DJGray
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 26

Accepted Solution

Anurag Thakur earned 250 total points
ID: 24396338
you can use the time class from the following link to achieve the functionality
LVL 16

Expert Comment

ID: 24396407
Hmm. Your code looked ok to me. I ran a quick test and it seems to work fine for me.

I set the end date to an earlier time and it evaluated as being outside the range.

		DateTime dtStart = DateTime.Parse("5/15/2009 7:00:00 AM");
		DateTime dtEnd = DateTime.Parse("5/15/2009 12:30:00 PM");
		if (DateTime.Compare(DateTime.Now, dtStart) > 0 && DateTime.Compare(DateTime.Now, dtEnd) < 0)
			Console.WriteLine("The current Time falls WITHIN the test range.");
			Console.WriteLine("The current Time falls OUTSIDE the test range.");

Open in new window

LVL 63

Expert Comment

by:Fernando Soto
ID: 24396493
Hi DJGray;

The code snippet below worked for me. It is using your if statement. Can you post how you are initializing dtStart and dtEnd.

DateTime dtStart = new DateTime(2009, 5, 15, 7, 0, 0);
DateTime dtEnd = new DateTime(2009, 5, 15, 12, 30, 0);
if (DateTime.Compare(DateTime.Now, dtStart) > 0 && DateTime.Compare(DateTime.Now, dtEnd) < 0)
    Console.WriteLine("I am in the time frame");
    Console.WriteLine("I am NOT in the  time frame");

Open in new window

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 24396902
Sure.  I have a DBAccessor  class I call thusly:

 DateTime dtStart = myDB.GetStartTime(myConn);
 DateTime dtEnd = myDB.GetStopTime(myConn);

Those calls are fed like this:
 internal DateTime GetStartTime(SqlConnection conn)
     SqlCommand myCmd = new SqlCommand("Select StartTime from ServerStatus where ServerStatusId = 1", conn);
     DateTime StartTime = (DateTime)myCmd.ExecuteScalar();
     return StartTime;

When I get some time, I'm going to look at the class that ragi0017 linked to.  


Expert Comment

ID: 24397323
Try this and let me know if this works.

startDate and endDate can be any valid dates either pulled from database or textbox etc.,
    DateTime startDate = Convert.ToDateTime("5/15/2009 7:00:00 AM");
        DateTime endDate = Convert.ToDateTime("5/15/2009 4:00:00 PM");
        if (DateTime.Now > startDate && DateTime.Now < endDate)

Open in new window

LVL 63

Expert Comment

by:Fernando Soto
ID: 24397360
Also place some


Before the if statement to confirm that the dates are what you think they are.

Author Closing Comment

ID: 31581943
Generally, I'm not a fan of the "link to" solutions, but in this case, the link is spot on.  It's exactly what I needed, and I'm surprised I couldn't find it with multiple Google searches.

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VS2010 Build fails to install 14 101
orderby list (from Json) 1 43
Using sample c# simple example 1 53
Example code 13 42
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below.…
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question