且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C# - 两个日期之间的差异?

更新时间:2023-02-07 18:00:07

  INT天数=(int)的Math.Ceiling(diff.TotalDays ); 


I am trying to calculate the difference between two dates. This is what I'm currently using:

int currentyear = DateTime.Now.Year;

DateTime now = DateTime.Now;
DateTime then = new DateTime(currentyear, 12, 26);
TimeSpan diff = now - then;
int days = diff.Days;
label1.Text = days.ToString() + " Days Until Christmas";

All works fine except it is a day off. I am assuming this is because it does not count anything less than 24 hours a complete day. Is there a way to get it to do so? Thank you.

int days = (int)Math.Ceiling(diff.TotalDays);