How can I calculate the difference between two dates?

后端 未结 9 1352
渐次进展
渐次进展 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:10
    NSDate *date1 = [NSDate dateWithString:@"2010-01-01 00:00:00 +0000"];
    NSDate *date2 = [NSDate dateWithString:@"2010-02-03 00:00:00 +0000"];
    
    NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];
    
    int numberOfDays = secondsBetween / 86400;
    
    NSLog(@"There are %d days in between the two dates.", numberOfDays);
    

    EDIT:

    Remember, NSDate objects represent exact moments of time, they do not have any associated time-zone information. When you convert a string to a date using e.g. an NSDateFormatter, the NSDateFormatter converts the time from the configured timezone. Therefore, the number of seconds between two NSDate objects will always be time-zone-agnostic.

    Furthermore, this documentation specifies that Cocoa's implementation of time does not account for leap seconds, so if you require such accuracy, you will need to roll your own implementation.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-28 03:13

    Swift 4
    Try this and see (date range with String):

    // Start & End date string
    let startingAt = "01/01/2018"
    let endingAt = "08/03/2018"
    
    // Sample date formatter
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "dd/MM/yyyy"
    
    // start and end date object from string dates
    var startDate = dateFormatter.date(from: startingAt) ?? Date()
    let endDate = dateFormatter.date(from: endingAt) ?? Date()
    
    
    // Actual operational logic
    var dateRange: [String] = []
    while startDate <= endDate {
        let stringDate = dateFormatter.string(from: startDate)
        startDate = Calendar.current.date(byAdding: .day, value: 1, to: startDate) ?? Date()
        dateRange.append(stringDate)
    }
    
    print("Resulting Array - \(dateRange)")
    

    Swift 3

    var date1 = Date(string: "2010-01-01 00:00:00 +0000")
    var date2 = Date(string: "2010-02-03 00:00:00 +0000")
    var secondsBetween: TimeInterval = date2.timeIntervalSince(date1)
    var numberOfDays: Int = secondsBetween / 86400
    print(numberOfDays)
    
    0 讨论(0)
  • 2020-11-28 03:13

    If you want all the units, not just the biggest one, use one of these 2 methods (based on @Ankish's answer):

    Example output: 28 D | 23 H | 59 M | 59 S

    + (NSString *) remaningTime:(NSDate *)startDate endDate:(NSDate *)endDate
    {
        NSCalendarUnit units = NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
        NSDateComponents *components = [[NSCalendar currentCalendar] components:units fromDate: startDate toDate: endDate options: 0];
        return [NSString stringWithFormat:@"%ti D | %ti H | %ti M | %ti S", [components day], [components hour], [components minute], [components second]];
    }
    
    + (NSString *) timeFromNowUntil:(NSDate *)endDate
    {
        return [self remaningTime:[NSDate date] endDate:endDate];
    }
    
    
    0 讨论(0)
  • 2020-11-28 03:19
    NSTimeInterval diff = [date2 timeIntervalSinceDate:date1]; // in seconds
    

    where date1 and date2 are NSDate's.

    Also, note the definition of NSTimeInterval:

    typedef double NSTimeInterval;
    
    0 讨论(0)
  • 2020-11-28 03:20

    With Swift 5 and iOS 12, according to your needs, you may use one of the two following ways to find the difference between two dates in days.


    #1. Using Calendar's dateComponents(_:from:to:) method

    import Foundation
    
    let calendar = Calendar.current
    
    let startDate = calendar.date(from: DateComponents(year: 2010, month: 11, day: 22))!
    let endDate = calendar.date(from: DateComponents(year: 2015, month: 5, day: 1))!
    
    let dateComponents = calendar.dateComponents([Calendar.Component.day], from: startDate, to: endDate)
    
    print(dateComponents) // prints: day: 1621 isLeapMonth: false
    print(String(describing: dateComponents.day)) // prints: Optional(1621)
    

    #2. Using DateComponentsFormatter's string(from:to:) method

    import Foundation
    
    let calendar = Calendar.current
    
    let startDate = calendar.date(from: DateComponents(year: 2010, month: 11, day: 22))!
    let endDate = calendar.date(from: DateComponents(year: 2015, month: 5, day: 1))!
    
    let formatter = DateComponentsFormatter()
    formatter.unitsStyle = .full
    formatter.allowedUnits = [NSCalendar.Unit.day]
    
    let elapsedTime = formatter.string(from: startDate, to: endDate)
    print(String(describing: elapsedTime)) // prints: Optional("1,621 days")
    
    0 讨论(0)
提交回复
热议问题