从 NSDate 获取到最后一个 UTC 午夜的时间间隔
Get interval to last UTC midnight from an NSDate
需要比较两次,一次是当前的NSDate
,一次是NSTimeInterval
。当自上次 UTC 午夜以来的当前时间间隔比给定时间间隔 大 时,我需要触发一个动作。我很满意
NSDate *rightNowUTC = [[NSDate alloc] init];
获取当前 UTC NSDate
,但我需要将其剥离为 "the time interval between whatever *now is and the last time midnight UTC occurred"。
NSCalendar
提供了一种获取给定日期一天(午夜)开始的方法:
NSDate *rightNowUTC = [[NSDate alloc] init];
NSDate *midnight = [NSCalendar.currentCalendar startOfDayForDate: rightNowUTC];
NSTimeInterval interval = [rightNowUTC timeIntervalSinceDate: midnight];
需要比较两次,一次是当前的NSDate
,一次是NSTimeInterval
。当自上次 UTC 午夜以来的当前时间间隔比给定时间间隔 大 时,我需要触发一个动作。我很满意
NSDate *rightNowUTC = [[NSDate alloc] init];
获取当前 UTC NSDate
,但我需要将其剥离为 "the time interval between whatever *now is and the last time midnight UTC occurred"。
NSCalendar
提供了一种获取给定日期一天(午夜)开始的方法:
NSDate *rightNowUTC = [[NSDate alloc] init];
NSDate *midnight = [NSCalendar.currentCalendar startOfDayForDate: rightNowUTC];
NSTimeInterval interval = [rightNowUTC timeIntervalSinceDate: midnight];