iphone - 两个日期之间缺少月份名称

标签 iphone ios nsdate nscalendar

我正在使用这个代码

int unitFlags =  NSMonthCalendarUnit;   
NSDateComponents *comps = [gregorian components:unitFlags fromDate:startDate toDate:endDate options:0];    
int months = [comps month];

我实际上想要缺少任何想法的月份名称?

最佳答案

我已经用两种方法解决了这个问题,我不知道哪个更好:(我期待人们对我的答案发表评论)

因为你想要开始和结束日期之间的所有月份名称,你可以这样做:

方法一:

NSString *startString=@"23 Jun 2012";
NSString *endString=@"23 Dec 2013";

NSDateFormatter *dateFormatterDDMMMYYYY=[NSDateFormatter new];
[dateFormatterDDMMMYYYY setDateFormat:@"dd MMM yyyy"];

NSDate *startDate=[dateFormatterDDMMMYYYY dateFromString:startString];
NSDate *endDate=[dateFormatterDDMMMYYYY dateFromString:endString];

NSMutableArray *betweenMonths=[NSMutableArray new];
NSDate *tempDate=startDate;
while (tempDate<endDate) {
    NSDateFormatter *dateFormatterMMM=[NSDateFormatter new];
    [dateFormatterMMM setDateFormat:@"MMM"];
    NSString *monthName=[dateFormatterMMM stringFromDate:tempDate];
    [betweenMonths addObject:monthName];

    //tempDate is calculted after storing because you not need the last month.
    NSCalendar *calendar=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components=[NSDateComponents new];
    components.month=1;
    tempDate=[calendar dateByAddingComponents:components toDate:tempDate options:0];
}
NSLog(@"Month Name : %@",betweenMonths);

方法二:

NSString *startString=@"23 Jun 2012";
NSString *endString=@"23 Dec 2013";

NSDateFormatter *dateFormatterDDMMMYYYY=[NSDateFormatter new];
[dateFormatterDDMMMYYYY setDateFormat:@"dd MMM yyyy"];

NSDate *startDate=[dateFormatterDDMMMYYYY dateFromString:startString];
NSDate *endDate=[dateFormatterDDMMMYYYY dateFromString:endString];

NSInteger month = [[[NSCalendar currentCalendar] components: NSMonthCalendarUnit
                                                   fromDate: startDate
                                                     toDate: endDate
                                                    options: 0] month];

NSDateFormatter *dateFormatterMMM=[NSDateFormatter new];
[dateFormatterMMM setDateFormat:@"MMM"];
NSString *startMonth=[dateFormatterMMM stringFromDate:startDate];

NSArray *months=@[@"Jan",@"Feb",@"Mar",@"Apr",@"May",@"Jun",@"Jul",@"Aug",@"Sep",@"Oct",@"Nov",@"Dec"];

NSInteger startDateMonth=[months indexOfObject:startMonth];

for (NSInteger i=startDateMonth+1; i<startDateMonth+month; i++) {
    NSLog(@"Moths : %@",months[i%12]); //%12 for checking if i goes beyond 11, as endDate may be more then one year span long

}

关于iphone - 两个日期之间缺少月份名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14302038/

相关文章:

iphone - 是否有一个库或框架可以使用漂亮的箭头和手写创建注释?

iphone - DISPATCH_SOURCE_TYPE_TIMER 未触发

ios - 空气原生扩展 : iOS packaged dependencies (Firebase)

cocoa-touch - 创建今天特定小时和分钟的 NSDate

iphone - 在 iOS 方法内部,未设置字典的值

iphone - 是否可以在 iOS 上下载和上传文档?

ios - 无互联网连接时的 URLSession 响应

ios - 为什么我的iPhone XR屏幕尺寸是375×812?

ios - objective-c dateFromString 在特定日期返回 nil

ios - 如何检测ios中的系统时间格式变化?