DateTime.Compare如何检查日期不到30天? [英] DateTime.Compare how to check if a date is less than 30 days old?

查看:110
本文介绍了DateTime.Compare如何检查日期不到30天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想工作,如果一个帐户在不到30天到期。我使用的DateTime比较正确?

 如果(DateTime.Compare(expiryDate,现)小于30)

{
     matchFound = TRUE;
}
 

解决方案
  

我使用的DateTime比较正确?

没有。 比较仅能提供约两个日期的相对位置的信息:小于,等于或大于。你需要的是这样的:

  IF((expiryDate  -  DateTime.Now).TotalDays小于30)
    matchFound = TRUE;
 

本减去两个的DateTime 秒。其结果是一个<一个href="http://msdn.microsoft.com/en-us/library/system.timespan%28VS.80%29.aspx"><$c$c>TimeSpan对象,它有一个 TotalDays 属性。

此外,有条件可直接写为:

  matchFound =(expiryDate  -  DateTime.Now).TotalDays&LT; 30;
 

没有如果必要的。

I'm trying to work out if an account expires in less than 30 days. Am I using DateTime Compare correctly?

if (DateTime.Compare(expiryDate, now) < 30)

{
     matchFound = true;
}

解决方案

Am I using DateTime Compare correctly?

No. Compare only offers information about the relative position of two dates: less, equal or greater. What you want is something like this:

if ((expiryDate - DateTime.Now).TotalDays < 30)
    matchFound = true;

This subtracts two DateTimes. The result is a TimeSpan object which has a TotalDays property.

Additionally, the conditional can be written directly as:

matchFound = (expiryDate - DateTime.Now).TotalDays < 30;

No if needed.

这篇关于DateTime.Compare如何检查日期不到30天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆