tags:

views:

303

answers:

7

In C# I need to compare the value of DateTime.Today /6pm, to a field that stores the Created DateTime.

Basically there is certain functionality that is only accessible on the same day as the created day and then only till 6pm.

The part I am not fully understanding is how to accurately represent 6pm on Today to compare against. Is there a method that always returns, say, Midnight that I can then do a .AddHours(18); to?

Am I over-complicating this? Thanks.

+8  A: 
DateTime SixPmToday = DateTime.Now.Date.AddHours(18);

If you output this, say to console, you will have (in my regional settings):

5/24/2010 6:00:00 PM
Kyle Rozendo
Q: How many hours are there between midnight and 6:00pm?A: 17, 18, or 19, depending on what day it is, and whether you are in a time zone that practices daylight savings time.
Jay Elston
It simply means we should get rid of DST and go back to letting high noon be high noon.
Anthony Pegram
@Downvoter - Care to mention why I was downvoted?
Kyle Rozendo
Not to derail my own question but this brings up an interesting side question. If Today returns 12am of today then wouldn't addhour(18) always return 6pm irregardless of DST? We are first ignoring DST when we choose to start at 12am and unless I am mistaken there are always 18 hours in between 12am and 6pm.....? Thoughts?
Refracted Paladin
`Add`/`Subtract` ignore time zones. So, `.AddHours(18)` on `DateTime.Today` or `DateTime.Now.Date` will always return a `DateTime` with the hours set to 18 / 6pm. See Coding Best Practices Using DateTime in the .NET Framework: http://msdn.microsoft.com/en-us/library/ms973825.aspx
R. Bemrose
More specifically, the Don't Get Fooled Again section: http://msdn.microsoft.com/en-us/library/ms973825.aspx#datetime_topic3a
R. Bemrose
@R. Bemrose, good link. It shows that if you *wanted* to recognize DST, you could do something like `someDate.ToUniversalTime().AddHours(3).ToLocalTime()`, however (as you point out) this is not relevant to this particular problem.
Anthony Pegram
+5  A: 

try this:

var n =DateTime.Now;
var today_6pm = new DateTime(n.Year, n.Month, n.Day, 18,0,0)
luke
This is the best one so far because of DST issues.
Bryan
There is no DST issue. DateTime arithmetic methods ignore DST. Midnight + 18 hours is always 6pm on all dates as long as the DateTime.Kind is local or unspecified.
Erv Walter
@Erv Walter - you are wrong about this. From the link mentioned elsewhere (http://msdn.microsoft.com/en-us/library/ms973825.aspx#datetime_topic6):"On these days, you can encounter conditions where the day is 23 or 25 hours in length. So if you are adding or subtracting spans of time from date values and the span crosses this strange point in time where the clocks switch, your code needs to make a manual adjustment."
Charles Boyung
+7  A: 

You can use DateTime.Today.AddHours(18). DateTime.Today gets only the current date.

Yuriy Faktorovich
+1 I like this better then the answer by @Kyle Rozendo
Oded
@Oded - A little less verbose. It is however identical. I just completely forgot about `.Today` :)
Kyle Rozendo
I'd prefer the other approach if only because it's practical for *any* date, be it today, tomorrow, or November 5, 1955.
Anthony Pegram
@Anthony Pegram - it is a closer semantic match to what the OP asked (18:00 _today_)
Oded
+3  A: 
DateTime created; //get this from wherever

DateTime midnight = DateTime.Today; //DateTime.Today returns today's date at midnight
DateTime sixpm = midnight.AddHours(18);

if (created >= midnight && created <= sixpm)
{
    // created is today and prior to 6pm
}
Erv Walter
I am curious why this one was downvoted? It is verbose but it is also the only one addressing my whole situation. I am not about to downvote the others for being succinct but I am curious if this method is technically flawed...seems like what I want overall...+1
Refracted Paladin
A: 

DateTime.Today.AddHours(18);

DateTime.Today returns a DateTime with the current date and 12:00 am as the time.

+1  A: 

This is the solution.

var creationDate = ... // fetch the creation date from somewhere
var availableUntil = creationDate.Date.AddHours(18);
if (DateTime.Now <= availableUntil)
{
    // The functionality is available.
}

This checks if it is before 6 PM on the creation date's day.

Venemo
A: 

How about this?

public static bool IsBefore6PM(System.DateTime _date)
{
     if(_date.CompareTo(System.DateTime.Today.AddHours(18)) < 0 && _date.CompareTo(System.DateTime.Today) >= 0)
     {
          return true;
     }  
     else
     {
          return false;
     }
}

This could also be modified if say a valid date is between 8am to 6pm.

public static bool IsBefore6PM(System.DateTime _date)
    {
         if(_date.CompareTo(System.DateTime.Today.AddHours(18)) < 0 && _date.CompareTo(System.DateTime.Today.AddHours(8)) >= 0)
         {
              return true;
         }  
         else
         {
              return false;
         }
    }
Tester101