需求比較兩個DateTime類型的年月
類似:只能是當月或當月之前的數據(只能是1號)
思路:將兩個需要對比的日期 全部轉換成月份 再做比較
如:DateTime dt_Now=new DateTime.Now();
string dt_other="2020-03-02";
DateTime othertime = DateTime.Parse(dt_other);//轉換日期
int now_Month = dtNow.Year * 12 + dtNow.Month;
int other_Month = dt_other.Year * 12 + dt_other.Month;
比較 now_Month 和 other_Month 大小即可。
代碼並未實現以上需求 僅記錄思路。