質問

how i can calculate the NSTimeInterval of today with hour minute and seconds to zero?

i know that the NSTimeInterval of today it's this:

NSTimeInterval  today = [[NSDate date] timeIntervalSince1970];

but how i can reset the time?

役に立ちましたか?

解決

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];

他のヒント

You need to use NSDateComponents to get only the year, month and day of today, then you can create a date from them which will be at midnight.

Once you have NSTimeInterval of now invoke the following function:

inline NSTimeInterval trimTimeInterval(NSTimeInterval interval)
{
    static NSTimeInterval const day = 3600 * 24;
    return day * floor(interval / day);
}

First we calculate the amount of integer days in interval and then get the same value back with the exception of the time element.

Note however, that this code won't account for the timezone. So you may end up with 23 May instead of 24 May for GMT+N>0.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top