C# DateTime.Compare 如何检查日期是否小于 30 天?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/528368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DateTime.Compare how to check if a date is less than 30 days old?
提问by
I'm trying to work out if an account expires in less than 30 days. Am I using DateTime Compare correctly?
我正在尝试计算帐户是否在 30 天内到期。我是否正确使用 DateTime 比较?
if (DateTime.Compare(expiryDate, now) < 30)
{
matchFound = true;
}
采纳答案by Konrad Rudolph
Am I using DateTime Compare correctly?
我是否正确使用 DateTime 比较?
No. Compare
only offers information about the relative position of two dates: less, equal or greater. What you want is something like this:
不,Compare
仅提供有关两个日期的相对位置的信息:小于、等于或大于。你想要的是这样的:
if ((expiryDate - DateTime.Now).TotalDays < 30)
matchFound = true;
This subtracts two DateTime
s. The result is a TimeSpan
object which has a TotalDays
property.
这减去两个DateTime
s。结果是一个TimeSpan
具有TotalDays
属性的对象。
Additionally, the conditional can be written directly as:
此外,条件可以直接写成:
matchFound = (expiryDate - DateTime.Now).TotalDays < 30;
No if
needed.
不需要if
。
回答by Timothy Carter
No, the Compare function will return either 1, 0, or -1. 0 when the two values are equal, -1 and 1 mean less than and greater than, I believe in that order, but I often mix them up.
不,比较函数将返回 1、0 或 -1。0 当两个值相等时,-1 和 1 表示小于和大于,我相信这个顺序,但我经常把它们混在一起。
回答by David Basarab
No you are not using it correctly.
不,您没有正确使用它。
See herefor details.
有关详细信息,请参见此处。
DateTime t1 = new DateTime(100);
DateTime t2 = new DateTime(20);
if (DateTime.Compare(t1, t2) > 0) Console.WriteLine("t1 > t2");
if (DateTime.Compare(t1, t2) == 0) Console.WriteLine("t1 == t2");
if (DateTime.Compare(t1, t2) < 0) Console.WriteLine("t1 < t2");
回答by JaredPar
Try this instead
试试这个
if ( (expiryDate - DateTime.Now ).TotalDays < 30 ) {
matchFound = true;
}
回答by GWLlosa
What you want to do is subtract the two DateTimes (expiryDate and DateTime.Now). This will return an object of type TimeSpan. The TimeSpan has a property "Days". Compare that number to 30 for your answer.
您要做的是减去两个 DateTimes(expiryDate 和 DateTime.Now)。这将返回一个 TimeSpan 类型的对象。TimeSpan 有一个属性“天”。将该数字与 30 进行比较以获得您的答案。
回答by Canavar
No it's not correct, try this :
不,这是不正确的,试试这个:
DateTime expiryDate = DateTime.Now.AddDays(-31);
if (DateTime.Compare(expiryDate, DateTime.Now.AddDays(-30)) < 1)
{
matchFound = true;
}
回答by Mitch Wheat
Compare returns 1, 0, -1 for greater than, equal to, less than, respectively.
比较对于大于、等于、小于分别返回 1、0、-1。
You want:
你要:
if (DateTime.Compare(expiryDate, DateTime.Now.AddDays(30)) <= 0)
{
bool matchFound = true;
}
回答by haqwin
Well I would do it like this instead:
好吧,我会这样做:
TimeSpan diff = expiryDate - DateTime.Today;
if (diff.Days > 30)
matchFound = true;
Compare only responds with an integer indicating weather the first is earlier, same or later...
比较只响应一个整数,指示第一个天气早、相同或晚...
回答by Luke
should be
应该
matchFound = (expiryDate - DateTime.Now).TotalDays < 30;
note the total days otherwise you'll get werid behaviour
注意总天数,否则你会得到奇怪的行为
回答by Jayant
This will give you accurate result :
这会给你准确的结果:
if ((expiryDate.Date - DateTime.Now.Date).Days < 30)
matchFound = true;