给定任何 NSDate,我将如何获得最后一个午夜和下一个午夜的日期?
这非常复杂,但可行:
NSDate *date = ...;
NSUInteger componentFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *parts = [[NSCalendar currentCalendar] components:componentFlags fromDate:date];
NSDateComponents *midnightComponents = [[NSDateComponents alloc] init];
[midnightComponents setDay:[parts day]];
[midnightComponents setMonth:[parts month]];
[midnightComponents setYear:[parts year]];
NSCalendar *cal = [[NSCalendar currentCalendar];
NSDate *midnight = [cal dateFromComponents:midnightComponents];
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:1];
NSDate *nextMidnight = [cal dateByAddingComponentsffsetComponents toDate: midnight options:0];
我认为与 .NET 甚至 Java 相比,Apple 的设计有点过于复杂,但它是易于管理的。
关于objective-c - 如何到达包含任何日期的午夜的 NSDates?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10623176/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) | Powered by Discuz! X3.4 |