开发者

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 othe开发者_如何学运维rDate.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),



                                   };


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.


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜