Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>This link has an example about how to get a date's weekday: </p> <p><a href="https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/Reference/NSCalendar.html#//apple_ref/doc/uid/TP40001451-SW3" rel="nofollow noreferrer">http://stackoverflow.com/questions/1057349#1057405</a></p> <p>Another approach as suggested by vikingosegundo to get both start and end days <a href="https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/Reference/NSCalendar.html#//apple_ref/doc/uid/TP40001451-SW3" rel="nofollow noreferrer"><code>rangeOfUnit:startDate:interval:forDate:</code></a>. It gives you the start and the interval for a certain time unit. With it it is easy to find the start of the week in the used calendar and add the range-1 to get the latest second in that week.</p> <pre><code>NSCalendar *cal = [NSCalendar currentCalendar]; NSDate *now = [NSDate date]; NSDate *startOfTheWeek; NSDate *endOfWeek; NSTimeInterval interval; [cal rangeOfUnit:NSWeekCalendarUnit startDate:&amp;startOfTheWeek interval:&amp;interval forDate:now]; //startOfWeek holds now the first day of the week, according to locale (monday vs. sunday) endOfWeek = [startOfTheWeek dateByAddingTimeInterval:interval-1]; // holds 23:59:59 of last day in week. </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload