Ignore milliseconds when comparing two datetimes

前端 未结 14 2314
一个人的身影
一个人的身影 2020-12-08 12:47

This is probably a dumb question, but I cannot seem to figure it out. I am comparing the LastWriteTime of two files, however it is always failing because the file I download

14条回答
  •  一个人的身影
    2020-12-08 13:28

    You could create an extension method that would set the milliseconds to zero for a DateTime object

    public static DateTime ZeroMilliseconds(this DateTime value) {
      return new DateTime(value.Year, value.Month, value.Day, 
        value.Hours, value.Minutes, value.Seconds);
    }
    

    Then in your function

     if (dtOrig.ZeroMilliseconds() == dtNew.ZeroMilliseconds())
            return true;
         else
            return false;
    

提交回复
热议问题