如何使用DateTime.Now.Hour?

时间:2011-08-14 02:21:15

标签: c# datetime

我想做什么:

我正试图在当前时间内从一个xml文件中删除电视列表。例如..时间现在是下午2点,我想在当前时间显示所有频道的所有列表。

以下获取当前日期并将其与XMl文件匹配并显示所有匹配项。我想在当前时间做同样的事情。

如果我将方法更改为以下,我会收到此错误:

错误1运算符'=='无法应用于'System.DateTime'和'int'类型的操作数

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

这是我目前用来在今天的日期显示它并且工作正常。

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

以下是更多使代码更清晰的代码。

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),



                                   };

2 个答案:

答案 0 :(得分:4)

您遇到的错误是因为您尝试将日期时间与一小时进行比较,而不仅仅是比较小时数。您无法直接将intDateTime进行比较。

以下是一些可行的代码:

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

如果您想在一小时内进行检查,并且不在乎是否匹配不同的日期,则可以将代码更改为:

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

对于将来的类似问题,我建议您look up the docs on the DateTime class确切了解每个属性返回的内容。

日期和时间处理通常比您最初的想法更复杂,并且需要对.Net框架如何处理时间有一些了解。查看文档并在单独的临时项目中进行一些实验通常很有帮助。

答案 1 :(得分:3)

这应该有效:

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

然而,这并不一定意味着同一天,所以也许你正在寻找:

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