ios - 在开始日期和结束日期之间获取每月的第三个星期六

标签 ios objective-c iphone nsdate nsdatecomponents

我想获取日期,即两个日期之间的月份的第三个星期六。我已经做了,但这不是我想要的。

NSInteger count = 0;
NSInteger saturday = 7;

// Set the incremental interval for each interaction.
NSDateComponents *oneDay = [[NSDateComponents alloc] init];
[oneDay setDay:1];

// Using a Gregorian calendar.
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDate *currentDate = [[UtilityClass sharedObject] stringToDate:@"2016-06-17" withFormate:@"yyyy-MM-dd"];
        NSDate *toDate = [[UtilityClass sharedObject] stringToDate:@"2016-08-17" withFormate:@"yyyy-MM-dd"];


// Iterate from fromDate until toDate
while ([currentDate compare:toDate] == NSOrderedAscending) {

NSDateComponents *dateComponents = [calendar components:NSWeekdayCalendarUnit fromDate:currentDate];
[dateComponents setWeekdayOrdinal:3];

if (dateComponents.weekday == saturday) {
    count++;
    NSLog(@"Date = %@",currentDate);
}

// "Increment" currentDate by one day.
currentDate = [calendar dateByAddingComponents:oneDay
                                                    toDate:currentDate
                                                   options:0];
}

在我的代码中,我已经成功获取了星期六,但是所有的星期六都在startdateenddate之间。
我也尝试这样:

[dateComponents setWeekdayOrdinal:3];
但不起作用。

这是我的日志
2016-06-17 11:29:18.319 Floacstation[1715:84848] Current Date :2016-06-16 18:30:00 +0000
2016-06-17 11:29:18.319 Floacstation[1715:84848] To Date : 2016-08-16 18:30:00 +0000

2016-06-17 11:29:18.320 Floacstation[1715:84848] Saturday Date = 2016-06-19 18:30:00 +0000
2016-06-17 11:29:18.320 Floacstation[1715:84848] Saturday Date = 2016-06-26 18:30:00 +0000
2016-06-17 11:29:18.320 Floacstation[1715:84848] Saturday Date = 2016-07-03 18:30:00 +0000
2016-06-17 11:29:18.320 Floacstation[1715:84848] Saturday Date = 2016-07-10 18:30:00 +0000
2016-06-17 11:29:18.320 Floacstation[1715:84848] Saturday Date = 2016-07-17 18:30:00 +0000
2016-06-17 11:29:18.320 Floacstation[1715:84848] Saturday Date = 2016-07-24 18:30:00 +0000
2016-06-17 11:29:18.320 Floacstation[1715:84848] Saturday Date = 2016-07-31 18:30:00 +0000
2016-06-17 11:29:18.321 Floacstation[1715:84848] Saturday Date = 2016-08-07 18:30:00 +0000
2016-06-17 11:29:18.321 Floacstation[1715:84848] Saturday Date = 2016-08-14 18:30:00 +0000

最佳答案

原因:

请注意,您已经输入了日期17th,但是它返回了16th ..因为无论何时您是UTCprinting NSDate in NSLog,它都在考虑converting it explicitly to NSString格式。

每当您想要字符串中的日期时,请使用NSDateFormatter,就像我在此代码中使用的一样。

您面临的问题就是因为这个。
因此,无论您在何处使用过Date,只需将replace the method of converting date放入String并再次检查Output。

试试这个...它必须给您正确的答案...

NSInteger count = 0;
NSInteger saturday = 7;

// Set the incremental interval for each interaction.
NSDateComponents *oneDay = [[NSDateComponents alloc] init];
[oneDay setDay:1];

// Using a Gregorian calendar.
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDate *currentDate = [[UtilityClass sharedObject] stringToDate:@"2016-06-17" withFormate:@"yyyy-MM-dd"];
        NSDate *toDate = [[UtilityClass sharedObject] stringToDate:@"2016-08-17" withFormate:@"yyyy-MM-dd"];


// Iterate from fromDate until toDate
while ([currentDate compare:toDate] == NSOrderedAscending) {

NSDateComponents *dateComponents = [calendar components:NSWeekdayCalendarUnit fromDate:currentDate];
[dateComponents setWeekdayOrdinal:3];

if (dateComponents.weekday == saturday) {
    count++;

    if (count==3) {
            NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
            formatter.dateFormat=@"dd MM YYYY";

            NSLog(@"Date = %@",[formatter stringFromDate:currentDate]);
        }
  }

}   

//无需增加此日期。
// "Increment" currentDate by one day.
// currentDate = [calendar dateByAddingComponents:oneDay
//                                                    toDate:currentDate
//                                                   options:0];

希望这可以帮助... :)

关于ios - 在开始日期和结束日期之间获取每月的第三个星期六,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37872819/

相关文章:

iphone - 如何对 UIImage 进行去饱和处理?

iphone - 为什么这个 CAKeyFrameAnimation 会以零不透明度停止?

ios - 在 GMAP iOS 中获取多个位置的路线。

ios - 在表格 View 单元格中显示事件指示器

ios - 从 NSURLConnection 迁移到 NSURLSession

objective-c - UIView Image 可以在其框架外绘制图像吗?

objective-c - 将 Firebase 引用发送到 Apple Watch

objective-c - 自定义 UIView 委托(delegate)未检测到 touchesBegan

ios - 使用 uicollectionview 执行类似 kindle 应用程序的动画

iOS 基础知识 : private property, 公共(public) setter / getter