Solved

C# working with TIME only, and no DATE

Posted on 2009-05-15
7
1,235 Views
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.
0
Comment
Question by:DJGray
7 Comments
 
LVL 26

Accepted Solution

by:
Anurag Thakur earned 250 total points
Comment Utility
you can use the time class from the following link to achieve the functionality
http://www.csharphelp.com/archives4/archive682.html
0
 
LVL 16

Expert Comment

by:ToddBeaulieu
Comment Utility
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.");

		else

			Console.WriteLine("The current Time falls OUTSIDE the test range.");

Open in new window

0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
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.

Fernando
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");

}

else

{

    Console.WriteLine("I am NOT in the  time frame");

}

Open in new window

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:DJGray
Comment Utility
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.  

0
 
LVL 3

Expert Comment

by:channa_m
Comment Utility
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)

        {

            Response.Write("Valid");

        }

        else

        {

            Response.Write("Invalid");

        }

Open in new window

0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Also place some

Console.WriteLine(dtStart.ToString());
Console.WriteLine(dtEnd.ToString());

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

Author Closing Comment

by:DJGray
Comment Utility
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.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now