Is there a better way to find midnight tomorrow?

前端 未结 6 2312
天涯浪人
天涯浪人 2021-02-13 22:05

Is there a better way to do this?

-(NSDate *)getMidnightTommorow {
    NSCalendarDate *now = [NSCalendarDate date];
    NSCalendarDate *tomorrow = [now dateByAdd         


        
6条回答
  •  耶瑟儿~
    2021-02-13 22:37

    You could try this way:

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setDay:1];
    NSDate *tomorrow = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0]; //it gives us tomorrow with current time
    NSDate *midnight = [calendar startOfDayForDate:tomorrow]; //here we get next midnight
    

    It is also easy to retrieve the seconds interval if needed to set up an NSTimer:

    double intervalToMidnight = midnight.timeIntervalSinceNow;
    

提交回复
热议问题