- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我希望显示来自 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]);
Đầu ra: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]);
Đầu ra:5 个月
虽然这在技术上是正确的,但我想四舍五入使其输出六个月。
有没有简单的方法可以达到这个效果?我注意到 NSDateComponents 上没有舍入属性。我是否必须手动检查天数并决定四舍五入?
我的最终目标不仅是将舍入效果限制在几个月内,如果我只提供:NSDayCalendarUnit
câu trả lời hay nhất
下面的方法可以达到你想要的效果。这个想法是在计算(向下舍入)日历单位数之后在开始日期和结束日期之间,将该金额加上一个到开始日期并检查哪一个更接近结束日期:
@interface NSCalendar (MyCategory)
-(NSInteger)roundedUnit:(NSCalendarUnit)unit fromDate:(NSDate *)fromDate toDate:(NSDate *)toDate;
@kết thúc
@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);
}
@kết thúc
你会用它作为
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/
我正在创建一个每周日历 View ,其中一部分是计算所显示的一周的第一天。我还希望保存的日期具有第一秒的时间,因此我试图确保日期的 hh:mm:ss 均为 00:00:00。 我有以下代码,在大多数情
我正在编写一些代码来获取服务器上的图像。图像文件名采用 yyyyMMdd_HHmm 格式(UTC),每 3 小时生成一次(1200,1500,1800,..等)。当代码启动时,我获取当前 UTC 日期
当我到达 2011 年 11 月 6 日时,我似乎无法弄清楚为什么这一天没有改变。我所做的只是遍历这些日子。任何帮助将不胜感激。这是我的代码: NSCalendar* calendar = [[NSC
我已经从Zaph阅读以下答案 iPhone - Correct way for getting current date and time for a given place / timezone a
如果您要运行此代码: // January 16th 2015 10:20 AM in Amsterdam var date = NSDate(timeIntervalSince1970: 14214
我有这段代码 NSDateComponents *comps = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCa
我正在使用多个 NSDateComponents 来显示一个时钟,其各个值在整个屏幕上被打散,但我遇到了格式化问题。 秒数显示为 1-60 而不是 00-59,小时数为 24 小时制而不是 12 小时
我有以下代码。当我在我的 iOS 3.1.3 设备上运行它时(克服它!)它按预期工作,但是当我在模拟器(iOS 5 和 4.3)上运行它时它会关闭 1 分 15 秒(9:01:15 ).这是因为我的代
我正在尝试获取两个日期之间的差异,它应该以天为单位显示。 NSLog(@"%@", [NSDate date]); NSDateFormatter *formatter = [[NSDateForma
我正在创建一个 NSDate。为什么一天总是少一天? 这是代码: NSDate *today = [NSDate date]; NSCalendar *gregorian = [[NSCalendar
为什么这给我错误的日期? NSCalendar *myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCa
我一直在使用 Swift 和 Xcode 6.3.2 开发一个时钟应用程序,以下代码构建和运行得很好。 // Get current time let date = NSDate() let cale
我正在尝试从 2 个变量获取数据:hours = "08" 和 minutes = 30。我是这样做的: let calendar = NSCalendar(identifier: NSCalenda
我的问题是处理下面代码片段中给出的 NSDateComponents 操作的时间配置文件。 我有一个迭代大量 NSDate 对象的方法 该方法的一部分要求我放弃每个日期的小时、分钟和秒数。 为此,我有
我试图在 NSDateComponents 中设置时间,我编写了以下代码 NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIden
我不明白为什么我不能使用 NSDateComponents 正确地更改 NSDate 的小时,我会这样做: NSDateComponent components = [calendar compone
一年有多少周?因为我在网上搜索过,我发现一年总共有 52.1 周,但是如果我在 Xcode 中这样做: NSCalendar *gregorian = [[NSCalendar alloc]
我希望显示来自 NSDate 对象的月数。 //Make Date Six Months In The Future NSCalendar *calendar = [[NSCalendar alloc
如果我有两个 nsdatecomponent 并且我想知道第一个对象中的 TIME 是否大于第二个。 例子: if (nsdatecomponentObject1 > nsdatecomponentO
我一直在使用 Swift 和 Xcode 6.3.2 开发一个时钟应用程序,以下代码构建和运行得很好。 // Get current time let date = NSDate() let cale
Tôi là một lập trình viên xuất sắc, rất giỏi!