简体   繁体   English

获取一周的开始和结束日期。 月份、周数和年份作为用户输入给出

[英]Get start and end date of a week. Month, week number, and year is given as user input

My code should bring the date of a particular week, when the user provide the month, week number, and year.当用户提供月份、星期数和年份时,我的代码应该带上特定星期的日期。 For example: If my user input is month (String) --> "October", week (Int) -- > 1, year (int) --> 2019, the above input should give the response like startDate --> 01/10/2019, endDate --> 07/10/2019.例如:如果我的用户输入是月份 (String) --> "October", week (Int) --> 1, year (int) --> 2019,上面的输入应该给出类似 startDate --> 01 的响应/10/2019,结束日期 --> 07/10/2019。

If the user give the last week of any month it should bring the remaining days in the month.如果用户给出任何一个月的最后一周,它应该带来该月的剩余天数。 Week calculated from Sunday.星期从星期日开始计算。

My attempted code giving start date and end date for current month / current我尝试的代码给出了当前月份/当前的开始日期和结束日期

NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

cal.firstWeekday = 1;// set first week day to Monday
// 1: Sunday, 2: Monday, ..., 7:Saturday

NSDate *now = [NSDate date];
NSDate *startOfTheWeek;
NSDate *endOfWeek;
NSTimeInterval interval;
[cal rangeOfUnit:NSCalendarUnitWeekOfYear
       startDate:&startOfTheWeek
        interval:&interval
         forDate:now];
//startOfTheWeek holds the beginning of the week

endOfWeek = [startOfTheWeek dateByAddingTimeInterval:interval - 1];
// endOfWeek now holds the last second of the last week day

[cal rangeOfUnit:NSCalendarUnitDay
       startDate:&endOfWeek
        interval:NULL
         forDate:endOfWeek];
// endOfWeek now holds the beginning of the last week day

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateStyle = NSDateFormatterShortStyle;
formatter.timeStyle = NSDateFormatterShortStyle;

NSLog(@"start: %@", [formatter stringFromDate:startOfTheWeek]);
NSLog(@"end:   %@", [formatter stringFromDate:endOfWeek]);

I came up with this that fulfills your criteria I think我想出了这个符合你的标准的我认为

NSInteger year = 2019;
NSInteger month = 11;
NSInteger week = 1;

NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

cal.firstWeekday = 2;

NSDateComponents *comps = [NSDateComponents new];
comps.calendar = cal;
comps.year = year;
comps.month = month;
comps.weekday = cal.firstWeekday;
comps.weekdayOrdinal = week - 1;

NSDateComponents *startOfTheMonthComps = [NSDateComponents new];
startOfTheMonthComps.year = year;
startOfTheMonthComps.month = month;
NSDateComponents *endOfTheMonthComps = [NSDateComponents new];
endOfTheMonthComps.year = year;
endOfTheMonthComps.month = month + 1;

NSDate *startOfTheMonth = [cal dateFromComponents:startOfTheMonthComps];
NSDate *endOfTheMonth = [[cal dateFromComponents:endOfTheMonthComps] dateByAddingTimeInterval:-1];

NSDate *date = [comps date];
NSDate *startOfTheWeek;
NSDate *endOfWeek;
NSTimeInterval interval;
[cal rangeOfUnit:NSCalendarUnitWeekOfYear
       startDate:&startOfTheWeek
        interval:&interval
         forDate:date];

endOfWeek = [startOfTheWeek dateByAddingTimeInterval:interval];

startOfTheWeek = [startOfTheWeek dateByAddingTimeInterval:-1];


if ([startOfTheWeek compare:startOfTheMonth] == NSOrderedAscending) {
    startOfTheWeek = startOfTheMonth;
}

if ([endOfTheMonth compare:endOfWeek] == NSOrderedAscending) {
    endOfWeek = endOfTheMonth;
}
NSLog(@"start %@", startOfTheWeek);
NSLog(@"end %@", endOfWeek);

So for 1st week of November you get Fri Nov 1 and Mon Nov 4 and for last week of September - week 6 Sun Sep 29 and Mon Sep 30因此,对于 11 月的第一周,您将获得Fri Nov 1Mon Nov 4 ,以及 9 月的最后一周 - 第 6 周,9 Sun Sep 29星期日和Mon Sep 30

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM