Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating first and last days of current week

Reference to pre-answered question at: Getting first and last days of current week

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

So regarding the question, how is it possible to get the PyObjC code translated to be compatible with iPhone SDK.

Target: Supposing today (Tue.) is 19th, and Sun. was 17th (start of week) and Sat. 23rd is end of week. I want to get a string like 19/01 - 23/01 [i.e. The start of week (hypen) end of week]

like image 602
Dot Avatar asked Jan 21 '26 01:01

Dot


2 Answers

If you have an NSDate, you can use the current NSCalendar to retrieve that date's NSDateComponents. Set the NSDateComponents's weekday to 1 (the first day of the week), create a copy, and set the copy's weekday to 7 (the last day of the week). Then use the NSCalendar to convert both the NSDateComponents back to their respective NSDate objects, after which you can use an NSDateFormatter to create your string representation.

This link has an example about how to get a date's weekday: https://stackoverflow.com/questions/1057349#1057405

like image 133
Dave DeLong Avatar answered Jan 23 '26 15:01

Dave DeLong


Here's some code and it also checks an edge case where the beginning of the week starts in the prior 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: Relevant code from 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];
}
like image 20
iwasrobbed Avatar answered Jan 23 '26 13:01

iwasrobbed