让我们假设:
static const unsigned long seconds_per_day = 60 * 60 * 24;
您可以通过以下方式获得现在和现在+24 小时:
NSDate* today = [NSDate date]; // set for today
NSDate* tomorrow = [today addTimeInterval:seconds_per_day];
为了获得给定日期的午夜,重要的是要记住它NSDate*
是免费桥接到的CFDateRef
,它提供了一些您想要使用的额外 API。特别是,您可以将 an 转换NSDate
为CFAbsoluteTime
with CFDateGetAbsoluteTime
:
CFAbsoluteTime abstime = CFDateGetAbsoluteTime(reinterpret_cast<CFDateRef>(myNSDate));
long long inttime = static_cast<long long>(abstime);
inttime = (inttime / seconds_per_day) * seconds_per_day; // clips the time to midnight
NSDate* myNSDateAtMidnight = reinterpret_cast<NSDate*>(CFDateCreate(NULL,
static_cast<CFAbsoluteTime>(inttime)));
...您可以将其包装成一个函数来舍入today
到tomorrow
午夜。