得到两个日期之间的差异? [英] Get difference between two dates?

查看:82
本文介绍了得到两个日期之间的差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在24小时内有两个约会,例如(dd-MMM-yyyy HH:mm).我想在这两个日期之间取得差价.

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

我的代码:这只是获取日期,我还希望有几天,剩余时间也要以小时为单位.(例如6天5个小时)

My Code : This is getting only dates, I want a number of days and remaining time in hours also. (like 6days 5 hours)

    NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];
    int numberOfDays = secondsBetween / 86400;
    NSLog(@"There are %d days in between the two dates.", numberOfDays);

推荐答案

NSDateComponentsFormatter 可以做到

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

有关其他内容,请参见 NSDateComponentsFormatterUnitsStyle 样式.

See NSDateComponentsFormatterUnitsStyle for other styles.

这篇关于得到两个日期之间的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆