NSDateComponents of an NSDate

后端 未结 4 680
广开言路
广开言路 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 08:47

    There's an example in the Apple docs, Listing 3: Getting a date’s components:

    NSDate *today = [NSDate date];
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *weekdayComponents =
                        [gregorian components:(NSDayCalendarUnit | 
                                               NSWeekdayCalendarUnit) fromDate:today];
    NSInteger day = [weekdayComponents day];
    NSInteger weekday = [weekdayComponents weekday];
    

    Note that you have to 'or' together the calendar units you want in the call of the components method.

提交回复
热议问题