[英]How to get day of month in number format?
我要查找的是月份中的某天,例如2、5、30或31。采用整數形式,而不是字符串。 我不是在尋找計划的日期,而是今天的日期(無論他們身在何處)。
以上所有答案都是實現此問題的壞方法。 這是正確的方法:
NSDate *date = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSDayCalendarUnit fromDate:date];
NSInteger day = components.day;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"d"];
NSInteger day = [[dateFormat stringFromDate:[NSDate date]] intValue];
這可能有效:
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
包含您要尋找的值!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.