获取具有给定月份的开始日期和结束日期的所有星期 (NSDate Objective C)
Get All Weeks with start date and end date of given month (NSDate Objective C)
我正在尝试列出给定月份的所有星期
像
开始日期 2020 年 9 月 6 日
结束日期 2020 年 9 月 12 日
如果一周在下个月结束,它也应该显示该日期
像
开始日期 2020 年 9 月 27 日
结束日期 2020 年 10 月 3 日
一个函数,它将 NSDate 作为输入和 returns 日期数组(一周的开始和结束日期)与 NSArray 或 NSDictionary 任何东西都可以工作只需要给定 NSDate 的开始和结束日期
我已经尝试过 DateTools、Datez 和其他 pods 但仍然无法得到这个
-(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 作为参数的函数和 returns 包含开始周日期和结束周日期的对象数组
您不需要使用第 3 方库来执行此操作,为此类事情提供了大量日期函数(主要参见 NSCalendar
的文档)。
这个答案在 Swift 中,但我会留给你找出转换。
let calendar = Calendar.current
let now = Date()
let monthInterval = calendar.dateInterval(of: .month, for: now)!
var weekIntervals: [DateInterval] = []
var startDate = monthInterval.start
while monthInterval.contains(startDate) {
weekIntervals.append(calendar.dateInterval(of: .weekOfMonth, for: startDate)!)
startDate = calendar.date(byAdding: .weekOfYear, value: 1, to: startDate)!
}
// Not sure why this is needed, seems like the above should handle it
let finalWeek = calendar.dateInterval(of: .weekOfMonth, for: startDate)!
if finalWeek.start < monthInterval.end {
weekIntervals.append(finalWeek)
}
打印结果给出...
let formatter = DateIntervalFormatter()
formatter.dateStyle = .medium
weekIntervals.forEach {
print(formatter.string(from: [=11=])!)
}
// Jul 26, 2020, 12:00 AM – Aug 2, 2020, 12:00 AM
// Aug 2, 2020, 12:00 AM – Aug 9, 2020, 12:00 AM
// Aug 9, 2020, 12:00 AM – Aug 16, 2020, 12:00 AM
// Aug 16, 2020, 12:00 AM – Aug 23, 2020, 12:00 AM
// Aug 23, 2020, 12:00 AM – Aug 30, 2020, 12:00 AM
// Aug 30, 2020, 12:00 AM – Sep 6, 2020, 12:00 AM
我正在尝试列出给定月份的所有星期 像 开始日期 2020 年 9 月 6 日 结束日期 2020 年 9 月 12 日
如果一周在下个月结束,它也应该显示该日期 像 开始日期 2020 年 9 月 27 日 结束日期 2020 年 10 月 3 日
一个函数,它将 NSDate 作为输入和 returns 日期数组(一周的开始和结束日期)与 NSArray 或 NSDictionary 任何东西都可以工作只需要给定 NSDate 的开始和结束日期
我已经尝试过 DateTools、Datez 和其他 pods 但仍然无法得到这个
-(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 作为参数的函数和 returns 包含开始周日期和结束周日期的对象数组
您不需要使用第 3 方库来执行此操作,为此类事情提供了大量日期函数(主要参见 NSCalendar
的文档)。
这个答案在 Swift 中,但我会留给你找出转换。
let calendar = Calendar.current
let now = Date()
let monthInterval = calendar.dateInterval(of: .month, for: now)!
var weekIntervals: [DateInterval] = []
var startDate = monthInterval.start
while monthInterval.contains(startDate) {
weekIntervals.append(calendar.dateInterval(of: .weekOfMonth, for: startDate)!)
startDate = calendar.date(byAdding: .weekOfYear, value: 1, to: startDate)!
}
// Not sure why this is needed, seems like the above should handle it
let finalWeek = calendar.dateInterval(of: .weekOfMonth, for: startDate)!
if finalWeek.start < monthInterval.end {
weekIntervals.append(finalWeek)
}
打印结果给出...
let formatter = DateIntervalFormatter()
formatter.dateStyle = .medium
weekIntervals.forEach {
print(formatter.string(from: [=11=])!)
}
// Jul 26, 2020, 12:00 AM – Aug 2, 2020, 12:00 AM
// Aug 2, 2020, 12:00 AM – Aug 9, 2020, 12:00 AM
// Aug 9, 2020, 12:00 AM – Aug 16, 2020, 12:00 AM
// Aug 16, 2020, 12:00 AM – Aug 23, 2020, 12:00 AM
// Aug 23, 2020, 12:00 AM – Aug 30, 2020, 12:00 AM
// Aug 30, 2020, 12:00 AM – Sep 6, 2020, 12:00 AM