How can I calculate the difference between two dates?

后端 未结 9 1354
渐次进展
渐次进展 2020-11-28 02:27

How can I calculate the days between 1 Jan 2010 and (for example) 3 Feb 2010?

9条回答
  •  自闭症患者
    2020-11-28 03:13

    You may want to use something like this:

    NSDateComponents *components;
    NSInteger days;
    
    components = [[NSCalendar currentCalendar] components: NSDayCalendarUnit 
            fromDate: startDate toDate: endDate options: 0];
    days = [components day];
    

    I believe this method accounts for situations such as dates that span a change in daylight savings.

提交回复
热议问题