开发者社区> 问答> 正文

关于NSTimeInterval时间零点 的问题

如何计算NSTimeInterval当日时间的时,分,秒为零点?

我只得到NSTimeInterval
`
NSTimeInterval today = [[NSDate date] timeIntervalSince1970];
`
如何重置为00:00:00?

展开
收起
爵霸 2016-03-23 08:42:18 2259 0
1 条回答
写回答
取消 提交回答
  • NSDate *currentDate = [NSDate date];
    NSDateComponents *currentDateComponents = [calendar components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:currentDate];
    NSDateComponents *currentDateToMidnight = [[NSDateComponents alloc] init];
    [currentDateToMidnight setHour:-[currentDateComponents hour]];
    [currentDateToMidnight setMinute:-[currentDateComponents minute]];
    [currentDateToMidnight setSecond:-[currentDateComponents second]];
    NSDate *midnight = [calendar dateByAddingComponents:currentDateToMidnight toDate:currentDate options:0];
    NSTimeInterval todayTimeInterval = [midnight timeIntervalSince1970];
    2019-07-17 19:10:12
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载