Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 比较当前月份c下的日期#_C#_Linq - Fatal编程技术网

C# 比较当前月份c下的日期#

C# 比较当前月份c下的日期#,c#,linq,C#,Linq,如何检查给定日期小于或等于当前月份。 i、 例如,任何小于或等于当前月份的datetime都应返回true。作为扩展方法: public static bool IsBeforeStartOfCurrentMonth(this DateTime date) { DateTime now = DateTime.Now; DateTime startOfCurrentMonth = new DateTime(now.Year, now.Month, 1); return da

如何检查给定日期小于或等于当前月份。
i、 例如,任何小于或等于当前月份的datetime都应返回true。

作为扩展方法:

public static bool IsBeforeStartOfCurrentMonth(this DateTime date) {
    DateTime now = DateTime.Now;
    DateTime startOfCurrentMonth = new DateTime(now.Year, now.Month, 1);
    return date < startOfCurrentMonth;
}
两种选择

1:查找月初并比较:

var monthStart = new DateTime(when.Year, when.Month, 1);
if(someDate < monthStart) {...}
var monthStart=new DateTime(when.Year,when.Month,1);
如果(someDate
2:比较月份和年份

if(someDate.Year < when.Year || (someDate.Year == when.Year &&
                         someDate.Month < when.Month)) {...}
if(someDate.Year

这两种方法都适用于
DateTime

上的扩展方法如果两个DateTime值-

DateTime d1, d2;
...
d1.Year <= d2.Year && d1.Month < d2.Month;
日期时间d1、d2;
...

d1.Year+1 Wierd-我得到了几乎完全相同的答案,包括变量名和所有内容。@Andrew,也许读了很多代码,所以,一个通用的命名系统会聚在一起:)哦..我遗漏了问题中的一部分,它不到等于当前月份
DateTime d1, d2;
...
d1.Year <= d2.Year && d1.Month < d2.Month;