Calculation of the first and last days of the current week

Link to the question asked: Getting the first and last days of the current week

There are two answers in the link above. One is theoretical, and the other is PyObjC (the Python-Objective-C interface language), and a quick Google search confirms that PyObjC does not work with the iPhone SDK.

So, regarding the question, how can I get the PyObjC code translated into iPhone SDK compatibility.

Purpose: Suppose today (Tue) - 19 and the Sun. was the 17th (beginning of the week) and Saturday. 23rd - the end of the week. I want to get a string like 19/01 - 23/01 [i.e. Beginning of the week (hypen) at the end of the week]

+3
source share
4 answers

If you have NSDate, you can use the current one NSCalendarto get this date NSDateComponents. Set the NSDateComponents weekdayvalue to 1 (first day of the week), create a copy and set the copy weekdayto 7 (last day of the week). Then use NSCalendarto convert both NSDateComponentsback to the corresponding objects NSDate, after which you can use NSDateFormatterto create a string representation.

This link provides an example of how to get the day of the week: fooobar.com/questions/422294 / ...

+7
source

Here's some code, and it also checks the case when the start of the week starts in the previous month:

// Finds the date for the first day of the week
- (NSDate *)getFirstDayOfTheWeekFromDate:(NSDate *)givenDate
{
    NSCalendar *calendar = [NSCalendar currentCalendar];

    // Edge case where beginning of week starts in the prior month
    NSDateComponents *edgeCase = [[NSDateComponents alloc] init];
    [edgeCase setMonth:2];
    [edgeCase setDay:1];
    [edgeCase setYear:2013];
    NSDate *edgeCaseDate = [calendar dateFromComponents:edgeCase];

    NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:edgeCaseDate];
    [components setWeekday:1]; // 1 == Sunday, 7 == Saturday
    [components setWeek:[components week]];

    NSLog(@"Edge case date is %@ and beginning of that week is %@", edgeCaseDate , [calendar dateFromComponents:components]);

    // Find Sunday for the given date
    components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:givenDate];
    [components setWeekday:1]; // 1 == Sunday, 7 == Saturday
    [components setWeek:[components week]];

    NSLog(@"Original date is %@ and beginning of week is %@", givenDate , [calendar dateFromComponents:components]);

    return [calendar dateFromComponents:components];
}

Edit: corresponding code above

- (NSDate *)firstDayOfWeekFrom:(NSDate *)givenDate {
  NSCalendar *calendar = [NSCalendar currentCalendar];

  NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:givenDate];
  [components setWeekday:1]; // 1 == Sunday, 7 == Saturday
  [components setWeekOfYear:[components weekOfYear]];

  return [calendar dateFromComponents:components];
}
+3

This is what I was looking for a few days ago and finally found. There is a way rangeOfUnit:startDate:interval:forDate:to NSDateand do it in a simple way. You can see the detailed information at: Current week Start and end date

0
source
NSDate *weekDate = [NSDate date];
NSCalendar *myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *currentComps = [myCalendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekOfYearCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:weekDate];
int ff = currentComps.weekOfYear;
NSLog(@"1  %d", ff);

[currentComps setWeekday:1]; // 1: sunday
NSDate *firstDayOfTheWeek = [myCalendar dateFromComponents:currentComps];
[currentComps setWeekday:7]; // 7: saturday
NSDate *lastDayOfTheWeek = [myCalendar dateFromComponents:currentComps];

NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
myDateFormatter.dateFormat = @"dd EEEE";
NSString *firstStr = [myDateFormatter stringFromDate:firstDayOfTheWeek];
NSString *secondStr = [myDateFormatter stringFromDate:lastDayOfTheWeek];

NSLog(@"first - %@ \nlast - %@", firstStr, secondStr);
0
source

Source: https://habr.com/ru/post/1728843/


All Articles