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

标签 c# linq

如何检查给定日期是否小于或等于当前月份。 即,任何小于或等于当前月份的日期时间都应返回 true。

最佳答案

作为扩展方法:

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

用法:

DateTime date = // some DateTime
if(date.IsBeforeStartOfCurrentMonth()) {
    // do something
}

关于c# - 比较当前月份以下的日期 c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4384738/

相关文章:

c# - 产量返回 brainfreeze

c# - 在更新方法中使用 LINQ - where 子句出现问题

c# sql 要处理什么

c# - 我如何在 C# 中使用 for each 循环 List<class> 中类的每一列

c# - 必须使用 dbcontext.Database.SqlQuery 声明标量变量 '@custid'?

java - scala -> 在同一程序中使用 .net (linq) 和 java 代码库?

c# - LINQ 在单列上分组

c# - 将列表的最后一个元素与之前的元素进行比较的简化语法?

c# - LINQ - 如何忽略 where 中的空列表

c# - 根据多个唯一字段从列表中选择