Rhys
Rhys

Reputation: 2877

How to use DateTime.Now.Hour?

What I am trying to do:

I am trying to disaply tv listings from a an xml file by the current hour. Eg.. time now is 2pm, I want to disaply all listings from all channels at this current hour.

The below get the current date and matches it to the XMl files and displays all matches. I want to do the same for current hour.

If I change the method to below, I get this error:

Error 1 Operator '==' cannot be applied to operands of type 'System.DateTime' and 'int'

  bool MyDateCheckingMethod(string dateString)
            {
                DateTime otherDate = DateTime.ParseExact(dateString, "yyyyMMddHHmmss K", null);
                // Is this today (ignoring time)?
                return otherDate.Date == DateTime.Now.Hour;
            }

This is what I am currently using to display by todays date and it works fine.

bool MyDateCheckingMethod(string dateString)
        {
            DateTime otherDate = DateTime.ParseExact(dateString, "yyyyMMddHHmmss K", null);
            // Is this today (ignoring time)?
            return otherDate.Date == DateTime.Now.Date;
        }

Here is more of the code to make it a little clearer.

void c_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
                return;


            var r = XDocument.Parse(e.Result);





            listBox2.ItemsSource = from tv in r.Root.Descendants("programme")
                                   where tv.Attribute("channel").Value == "1200"
                                   where MyDateCheckingMethod(tv.Attribute("start").Value)
                                   let channelE1 = tv.Attribute("channel")
                                   let startE1 = tv.Attribute("start")
                                   let nameEl = tv.Element("title")
                                   orderby tv.Attribute("start").Value ascending
                                   let urlEl = tv.Element("desc")


                                   select new TV1guide



                                   {
                                       DisplayName = nameEl == null ? null : nameEl.Value,
                                       ChannelName = channelE1 == null ? null : channelE1.Value,
                                       ChannelURL = urlEl == null ? null : urlEl.Value,
                                       StartTime = startE1 == null ? (DateTime?)null : DateTime.ParseExact(startE1.Value, "yyyyMMddHHmmss zzz", DateTimeFormatInfo.CurrentInfo, DateTimeStyles.AssumeLocal),



                                   };

Upvotes: 0

Views: 11037

Answers (2)

Merlyn Morgan-Graham
Merlyn Morgan-Graham

Reputation: 59151

The error you were getting is because you were trying to compare a date and time to an hour, rather than just comparing hours. You can't compare an int to a DateTime directly.

Here's some code that would work:

bool MyDateCheckingMethod(string dateString)
{
    DateTime otherDate = DateTime.ParseExact(dateString, "yyyyMMddHHmmss K", null);
    DateTime now = DateTime.Now;
    // Is this the current date and hour?
    return otherDate.Date == now.Date
        && otherDate.Hour == now.Hour;
}

If you want to do the check just on the hour, and don't care if you match different dates, you can change the code to this:

bool MyDateCheckingMethod(string dateString)
{
    DateTime otherDate = DateTime.ParseExact(dateString, "yyyyMMddHHmmss K", null);
    // Is this the current hour - regardless of date?
    return otherDate.Hour == DateTime.Now.Hour;
}

For similar problems in the future, I recommend that you look up the docs on the DateTime class to figure out exactly what each property returns.

Date and time handling is often more complicated than you'd initially think, and requires a bit of fore-knowledge on how the .Net framework handles time. It often is helpful to look to the documentation, and to do a bit of experimentation in a separate scratch project.

Upvotes: 4

Brian Mains
Brian Mains

Reputation: 50728

This should work:

return otherDate.Hour == DateTime.Now.Hour;

However, this doesn't necessarily mean the same day, so maybe you are looking for:

return otherDate.Date == DateTime.Now.Date &&
       otherDate.Hour == DateTime.Now.Hour;

Upvotes: 3

Related Questions