得到NSDate的月,日或年份?

时间:2010-08-02 18:03:50

标签: iphone objective-c

说我有以下内容:

NSDate *today = [NSDate date];

我如何只获得该月的获取整数值?天?还是一年?

2 个答案:

答案 0 :(得分:2)

你想要这个NSDateCOmponenets

具体是:

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

答案 1 :(得分:-1)

或者你可以使用NSDate类:

NSDate *date = [NSDate date];
    int month = [[date descriptionWithCalendarFormat:@"%m" timeZone:nil locale:nil] intValue];
    int day = [[date descriptionWithCalendarFormat:@"%d" timeZone:nil locale:nil] intValue];
    int year = [[date descriptionWithCalendarFormat:@"%Y" timeZone:nil locale:nil] intValue];

    NSLog(@"Today: %i-%i-%i", year, month, day);

希望它有效, ief2