【发布时间】:2020-12-08 02:17:54
【问题描述】:
我正在尝试列出给定月份的所有周 喜欢 开始日期 2020 年 9 月 6 日 结束日期 2020 年 9 月 12 日
如果一周在下个月结束,它也应该显示该日期 喜欢 开始日期 2020 年 9 月 27 日 结束日期 2020 年 10 月 3 日
一个将 NSDate 作为输入并使用 NSArray 或 NSDictionary 返回日期数组(一周的开始和结束日期)的函数,任何东西都可以工作,只需要给定 NSDate 的开始和结束日期
我尝试过 DateTools、Datez 和其他 pod,但仍然无法获得此功能
-(void) getWeeksOfTheMonthCurrent {
NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
for(int currentdateindexpath=0;currentdateindexpath<=10currentdateindexpath++)
{
NSDateComponents* subcomponent = [calendar components:NSCalendarUnitWeekday
fromDate:[NSDate date]];
[oneDayAgoComponents setDay: 0 - ([subcomponent weekday] - 1)];
NSDate *beginningOfWeek = [calendar dateByAddingComponents:oneDayAgoComponents
toDate:[NSDate date] options:0];
[formatter setDateFormat:@"dd/MM/yy"];
NSString *weekstartdate = [formatter stringFromDate:beginningOfWeek];
NSLog(@"weekstartdate %@",weekstartdate);
NSDateComponents *components =
[calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth |
NSCalendarUnitDay) fromDate: beginningOfWeek];
beginningOfWeek = [calendar dateFromComponents:components];
[oneDayAgoComponents setDay:7- ([subcomponent weekday])];
NSDate *endOfWeek = [calendar dateByAddingComponents:oneDayAgoComponents
toDate:[NSDate date] options:0];
[formatter setDateFormat:@"dd/MM/yy"];
NSString *weekendtdate = [formatter stringFromDate:endOfWeek];
NSLog(@"weekendtdate %@",weekendtdate);
}
}
我已尝试使用此代码获取开始周日期和结束周日期,但这不是我想要的 我只是想要一个以 NSDate 作为参数并返回包含开始周日期和结束周日期的对象数组的函数
【问题讨论】:
-
请展示您已经尝试过的内容、无效的内容、代码示例等。请阅读How to Ask 和minimal reproducible example 然后edit 您的问题显示最小值 演示问题的代码。
-
我更新了我的帖子
标签: ios objective-c nsdate nscalendar nsdatecomponents