How to clear milliseconds of NSDate

I want to save the date / time in a CoreData repository without seconds or milliseconds. (I do some processing to round the time, and stray seconds / milliseconds become the monkey key.) This is easy enough to drop the seconds:

NSDate *now = [NSDate date];
NSDateComponents *time = [[NSCalendar currentCalendar]
                          components:NSHourCalendarUnit | NSMinuteCalendarUnit 
                          | NSSecondCalendarUnit fromDate:now];
    NSDate *nowMinus = [now addTimeInterval:-time.second];
    // e.g. 29 Aug 10 4:43:05 -> 29 Aug 10 4:43:00

This works fine to reset seconds, but I cannot find NSMillisecondCalendarUnit, which I could use to reset milliseconds, and I need to. Any ideas? Thank.

+3
source share
1 answer

timeIntervalSince1970 ( ) 1 1970 . , , . , :

NSTimeInterval timeSince1970 = [[NSDate date] timeIntervalSince1970];

timeSince1970 -= fmod(timeSince1970, 60); // subtract away any extra seconds

NSDate *nowMinus = [NSDate dateWithTimeIntervalSince1970:timeSince1970];

, .

+7

Source: https://habr.com/ru/post/1762180/


All Articles