ios - NSDateComponents 四舍五入

标签 ios iphone objective-c xcode nsdatecomponents

我希望显示来自 NSDate 对象的月数。

//Make Date Six Months In The Future
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *sixMonthsFromNow = [[NSDateComponents alloc] init];
[sixMonthsFromNow setMonth:6];
NSDate *finishDate = [calendar dateByAddingComponents:sixMonthsFromNow toDate:[NSDate date] options:0]; //Six Months Time

//Display
NSCalendarUnit requiredFormat = NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *dateComponents  = [calendar components:requiredFormat fromDate:[NSDate date] toDate:finishDate options:0];

NSLog(@"%d months %d days %d hours %d minutes %d seconds", [dateComponents month], [dateComponents day], [dateComponents hour], [dateComponents minute], [dateComponents second]);

输出:5 个月 29 天 23 小时 59 分 59 秒

这很好,但我只想显示月数。

如果我只限制几个月:

//Make Date Six Months In The Future
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *sixMonthsFromNow = [[NSDateComponents alloc] init];
[sixMonthsFromNow setMonth:6];
NSDate *finishDate = [calendar dateByAddingComponents:sixMonthsFromNow toDate:[NSDate date] options:0]; //Six Months Time

//Display
NSCalendarUnit requiredFormat = NSMonthCalendarUnit;
NSDateComponents *dateComponents  = [calendar components:requiredFormat fromDate:[NSDate date] toDate:finishDate options:0];
NSLog(@"%d months", [dateComponents month]);

输出:5 个月

虽然这在技术上是正确的,但我想四舍五入使其输出六个月。

有没有简单的方法可以达到这个效果?我注意到 NSDateComponents 上没有舍入属性。我是否必须手动检查天数并决定四舍五入?

我的最终目标不仅是将舍入效果限制在几个月内,如果我只提供:NSDayCalendarUnit

,这应该能够将几小时四舍五入到几天

最佳答案

下面的方法可以达到你想要的效果。 这个想法是在计算(向下舍入)日历单位数之后 在开始日期和结束日期之间,将该金额加上一个到开始日期 并检查哪一个更接近结束日期:

@interface NSCalendar (MyCategory)
-(NSInteger)roundedUnit:(NSCalendarUnit)unit fromDate:(NSDate *)fromDate toDate:(NSDate *)toDate;
@end

@implementation NSCalendar (MyCategory)
-(NSInteger)roundedUnit:(NSCalendarUnit)unit fromDate:(NSDate *)fromDate toDate:(NSDate *)toDate
{
    // Number of units between the two dates:
    NSDateComponents *comps = [self components:unit fromDate:fromDate toDate:toDate options:0];
    NSInteger value = [comps valueForComponent:unit];

    // Add (value) units to fromDate:
    NSDate *date1 = [self dateByAddingComponents:comps toDate:fromDate options:0];

    // Add (value + 1) units to fromDate:
    [comps setValue:(value + 1) forComponent:unit];
    NSDate *date2 = [self dateByAddingComponents:comps toDate:fromDate options:0];

    // Now date1 <= toDate < date2. Check which one is closer,
    // and return the corresponding value:
    NSTimeInterval diff1 = [toDate timeIntervalSinceDate:date1];
    NSTimeInterval diff2 = [date2 timeIntervalSinceDate:toDate];
    return (diff1 <= diff2 ? value : value + 1);
}
@end

你会用它作为

NSInteger months = [calendar roundedUnit:NSMonthCalendarUnit fromDate:[NSDate date] toDate:finishDate];

代码使用 NSDateComponents 的实用方法来自 https://github.com/henrinormak/NSDateComponents-HNExtensions/blob/master/README.md :

- (void)setValue:(NSInteger)value forComponent:(NSCalendarUnit)unit;
- (NSInteger)valueForComponent:(NSCalendarUnit)unit;

这些方法是 OS X 10.9 中的新方法,但在 iOS 7 中不可用。

关于ios - NSDateComponents 四舍五入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21283592/

相关文章:

android - phonegap 构建应用程序的 Manifest.xml 和 config.xml 之间的区别

ios - 当键盘关闭时,如何使搜索栏中的取消按钮保持启用状态?

ios - Firebase CocoaPods 的重复符号链接(symbolic link)器错误

ios - 所有 UIView 过渡动画停止工作

ios - UIScrollView 以编程方式滚动到底部

iphone - 使用 View Controller 作为相机覆盖?

iphone - 将 facebook-json 与项目中现有的 JSON 库发生冲突

ios - 如何根据不同的形状拼贴图像

objective-c - NSScrollView 不绘制文档 View ?

iOS subview 与父 View 的引用关系