How to parse and extract Year, Month, Day etc from

2019-03-21 16:02发布

问题:

I am slowly getting into iOS development and trying to create a count up timer from a specific date. I have figured out the code which gives me the interval in seconds but I could not figure out how to extract Year/Month/Day/Hour/Minute/Second values from it in order to display each value in its own label as a ticker.

So far I have figured out that the following will give me the interval between the 2 dates in seconds, what I am trying to do is to parse this and display on my view this as a ticker by updating the UILabel every second using NSTimer and calling selector once every 1 seconds and get something like this on my view:

6Years 10Months 13Days 18Hours 25Minutes 18Seconds (obviously each label will be updated accordingly as the time goes up)

NSDate *startDate = [df dateFromString:@"2005-01-01"];

NSTimeInterval passed = [[NSDate date] timeIntervalSinceDate: startDate];

Thanks

回答1:

Use NSCalendar with the two dates:

- (NSDateComponents *)components:(NSUInteger)unitFlags fromDate:(NSDate *)startingDate toDate:(NSDate *)resultDate options:(NSUInteger)opts

Returns, as an NSDateComponents object using specified components, the difference between two supplied dates.

Example:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-dd"];
NSDate *startingDate = [dateFormatter dateFromString:@"2005-01-01"];
NSDate *endingDate = [NSDate date];

NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:startingDate toDate:endingDate options:0];

NSInteger days     = [dateComponents day];
NSInteger months   = [dateComponents month];
NSInteger years    = [dateComponents year];
NSInteger hours    = [dateComponents hour];
NSInteger minutes  = [dateComponents minute];
NSInteger seconds  = [dateComponents second];
NSLog(@"%dYears %dMonths %dDays %dHours %dMinutes %dSeconds", days, months, years, hours, minutes, seconds);

NSLog output:

13Years 10Months 6Days 8Hours 6Minutes 7Seconds


回答2:

Well, as "month" and "year" will of course depend on which month and which year it is (365 or 364 days in a year? 30 or 31 days in a month?), you cannot just take the time interval and extract years and months - you always have to get a full date, e.g. by adding your (modified) time interval back to the start date like

NSDate* newDate = [startDate dateByAddingTimeInterval:modifiedPassedTimeInterval];

From that date, you can easily extratc the year, month, and so on with NSDateComponents:

NSDateComponents* dateComponents = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:newDate];
NSInteger day = [dateComponents day];
NSInteger month = [dateComponents month];
NSInteger year = [dateComponents  year];