0

我要查找的是月份中的哪一天,例如 2、5、30 或 31。以整数形式,而不是字符串。我不是在寻找预定的日期,而是在他们所在的任何地方寻找今天的日期。

4

4 回答 4

3

以上所有答案都是实现此目的的不好方法。这是正确的方法:

NSDate *date = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSDayCalendarUnit fromDate:date];
NSInteger day = components.day;
于 2012-12-12T15:45:58.103 回答
2
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"d"];
NSInteger day = [[dateFormat stringFromDate:[NSDate date]] intValue];
于 2012-12-12T15:13:34.383 回答
0

这是可能有用的东西:

NSDate *theDate;

// Set theDate to the date you want
// For example, theDate = [NSDate date]; to get today's date

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"d";
NSString *theDayString = [formatter stringFromDate:theDate];
int theDay = theDayString.intValue;

NSLog (@"%d", theDay);

theDay包含您要查找的值!

于 2012-12-12T15:12:59.977 回答