如何从NSDate
获取月份和日期并保存为日期?我用谷歌搜索,但没有找到解决方案。
我尝试过类似的东西但没有用 -
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM"];
NSDate *dateFromString = [[NSDate alloc] init];
NSString *dateString = [arr objectAtIndex:0];
dateFromString = [dateFormatter dateFromString:dateString];
答案 0 :(得分:3)
你可以这样做:
NSDate *theDate = ...
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [gregorian components:NSMonthCalendarUnit | NSDayCalendarUnit
fromDate:theDate];
NSInteger mm = [comps month];
NSInteger dd = [comps day];