C# 如何使用DateTime.Now.Hour?
我想做的是: 我正在尝试在当前小时之前从xml文件中删除电视节目表。如。。现在的时间是下午2点,我想取消当前时间所有频道的所有节目表 下面的获取当前日期并将其与XMl文件匹配,并显示所有匹配项。我想在当前时间做同样的事情 如果我将方法更改为以下,则会出现此错误: 错误1运算符“==”不能应用于“System.DateTime”和“int”类型的操作数C# 如何使用DateTime.Now.Hour?,c#,datetime,C#,Datetime,我想做的是: 我正在尝试在当前小时之前从xml文件中删除电视节目表。如。。现在的时间是下午2点,我想取消当前时间所有频道的所有节目表 下面的获取当前日期并将其与XMl文件匹配,并显示所有匹配项。我想在当前时间做同样的事情 如果我将方法更改为以下,则会出现此错误: 错误1运算符“==”不能应用于“System.DateTime”和“int”类型的操作数 bool MyDateCheckingMethod(string dateString) {
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),
};
这应该起作用:
return otherDate.Hour == DateTime.Now.Hour;
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;
}
但是,这并不一定意味着同一天,因此您可能正在寻找:
return otherDate.Date == DateTime.Now.Date &&
otherDate.Hour == DateTime.Now.Hour;
您所遇到的错误是因为您试图将日期和时间与小时进行比较,而不仅仅是比较小时。不能直接将
int
与DateTime
进行比较
下面是一些可行的代码:
return otherDate.Hour == DateTime.Now.Hour;
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;
}
对于将来类似的问题,我建议您准确地计算每个属性返回的内容
日期和时间的处理通常比您最初想象的要复杂,并且需要一些关于.Net框架如何处理时间的预先知识。查看文档并在单独的scratch项目中进行一些实验通常是有帮助的。您是说
return otherDate.Date.Hour==DateTime.Now.Date.Hour代码>不编译?太好了,正是我想要的,我没有意识到我必须匹配其他date.hour和datetime.now.hour。谢谢你的快速回复,布莱恩,谢谢你的回复。我花了最后几个小时阅读MSDN中的DateTime,但是我想不出来。很难找到真实世界的教程来理解DateTime类,或者我的速度很慢:(有没有办法编辑它以包含+-2小时?那么,小时数匹配+-2小时的列表?@Rhys:如果有疑问,请创建一个单独的控制台项目,只添加一小部分您感到困惑的代码。编译它,并添加大量console.WriteLine
行。例如:console.WriteLine(DateTime.Now)
,控制台.WriteLine(DateTime.Now.GetType());
,控制台.WriteLine(DateTime.Now.Hour);
和控制台.WriteLine(DateTime.Now.Hour.GetType())
@Rhys:是的,但这更复杂,因为时间不多了……如果其他地方也没有答案,我也不会感到惊讶。你可能想点击页面顶部的搜索框。如果你过了一段时间没有找到任何东西,请随意发布另一个问题:)@Rhys:答案类似于(其他日期-现在)。持续时间