将截止日期与当前日期进行比较 [英] Comparing the due date with current date
本文介绍了将截止日期与当前日期进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是关于检查字段中选择的日期等于或大于(=>)到当前日期。所以我有字段ID(00001234)。现在我必须写一个if条件我认为.....任何人都可以帮助我,直到If条件,我会从那里开始,因为我很新,我很少与Dateformats混淆。
提前致谢。
我的尝试:
我尝试使用datetime.now,但是如果条件不正确则写错了。
Hi,
This is about checking the Date selected in the field is equal or greater than (=>)to current date. So i have the field ID (00001234). Now i have to write an if condition i think..... Can anyone help me till the If condition, i will take it forward from there as i am very new and i am little confused with Dateformats.
Thanks in advance.
What I have tried:
I tried using datetime.now but failed to write proper if condition.
推荐答案
理想情况下,当您比较日期时,您应该同时拥有这两个日期在相同的格式,否则你将得到奇怪的输出。这是一个用于日期比较的小片段。
Ideally when you compare dates you should have both the dates in same format otherwise you will end up with weird output. This is a small snippet for date comparison.
public void CompareDates()
{
string inputDate = "05/21/2016";
DateTime convertedDate = DateTime.Parse(inputDate);//will have time part as well.
DateTime nowDate = DateTime.Now;
//The following code will consider time part in date.
if (nowDate >= convertedDate)
{
//nowDate is greater or equal
}
else
{
//convertedDate is greater
}
//The following code will consider only date(no time)
if (nowDate.Date >= convertedDate.Date)
{
//nowDate is greater or equal
}
else
{
//convertedDate is greater
}
}
将调试器放在代码的最开头,看看编译器在每行上给你的内容。有关不同的日期格式,请参阅此 [ ^ ]和这个 [ ^ ]链接
这篇关于将截止日期与当前日期进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文