NSDateComponents of an NSDate

后端 未结 4 679
广开言路
广开言路 2020-12-17 08:07

How do I get the NSDateComponents of a single NSDate? I don\'t want the components of the difference between 2 dates, just the seconds, minutes, hours, day, month and year o

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-17 09:04

    using NSCalendar's method:

    - (NSDateComponents *)components:(NSUInteger)unitFlags fromDate:(NSDate *)date

    like:

    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
    NSDate *date = [NSDate date];
    
    
    NSCalendar * cal = [NSCalendar currentCalendar];
    NSDateComponents *comps = [cal components:unitFlags fromDate:date];
    

提交回复
热议问题