how to calculate number of weeks given 2 dates?

后端 未结 7 1945
耶瑟儿~
耶瑟儿~ 2020-12-11 15:45

I have two DateTime variables and i need to compute number of weeks between them.

What is the quickest (and correct) way to do this?

7条回答
  •  时光取名叫无心
    2020-12-11 15:53

    You could try the following:

    DateTime d1 = new DateTime(2006,10,1);
    DateTime d2 = new DateTime(2007,10,15);
    
    TimeSpan tt = d2 - d1;
    int totalWeeks = tt.Days/7;
    

    And if you want exact difference in fraction also then instead of:

    int totalWeeks = tt.Days/7;
    

    use:

    double totalWeeks = tt.TotalDays/7;
    

提交回复
热议问题