将时差与月份中的天数进行比较时出错

Error when comparing time difference with amount of days in month

我正在减去两个日期,然后将日差与一个月内的天数进行比较,但它崩溃了。这是代码:

if (Convert.ToInt32(DateTime.Now.Subtract(File.GetLastWriteTime(TestWorkbook(worksheets[i].Path) + ".xlsx").Date)) < DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month))

我收到的错误是:

Unable to cast object of type 'System.TimeSpan' to type 'System.IConvertible'

为什么会这样?

因此,如果我了解您要执行的操作,我会删除 Convert.ToInt32 并添加 .Days。这应该工作。所以尝试:

if (DateTime.Now.Subtract(File.GetLastWriteTime(TestWorkbook(worksheets[i].Path) + ".xlsx").Date).Days < DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month))
    //Do whatever