Get difference between two dates?

前端 未结 3 1778
情话喂你
情话喂你 2021-01-29 06:10

I have two dates in 24 hours formate like (dd-MMM-yyyy HH:mm). I want to get a difference between these two dates.

My Code : This is getting only dates, I want a number

3条回答
  •  无人及你
    2021-01-29 06:57

    NSDateComponentsFormatter can do that

    NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];
    NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init];
    formatter.allowedUnits = NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute;
    formatter.unitsStyle = NSDateComponentsFormatterUnitsStyleFull;
    NSLog(@"%@", [formatter stringFromTimeInterval:secondsBetween]);
    

    See NSDateComponentsFormatterUnitsStyle for other styles.

提交回复
热议问题