14

i.e.:

  NSDate *firstDayOfWeek = [[NSDate date] firstDayOfWeek];

for example, today is Aug 19, I'd want to get a NSDate of 2010-08-15 12:00am from above line of code. Thanks!

Suresh D
  • 4,303
  • 2
  • 29
  • 51
ohho
  • 50,879
  • 75
  • 256
  • 383

8 Answers8

36

I think this threads responds to what you're looking for: http://www.cocoabuilder.com/archive/cocoa/211648-nsdatecomponents-question.html#211826

Note however that it doesn't handle Mondays as first days of the week, so you may have to tweek it a little by substracting [gregorian firstWeekday] instead of just 1. Also, I modified it to use -currentCalendar, but it's up to you :-)

NSDate *today = [NSDate date];
NSCalendar *gregorian = [NSCalendar currentCalendar];

// Get the weekday component of the current date
NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:today];
/*
Create a date components to represent the number of days to subtract
from the current date.
The weekday value for Sunday in the Gregorian calendar is 1, so
subtract 1 from the number
of days to subtract from the date in question.  (If today's Sunday,
subtract 0 days.)
*/
NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init];
/* Substract [gregorian firstWeekday] to handle first day of the week being something else than Sunday */
[componentsToSubtract setDay: - ([weekdayComponents weekday] - [gregorian firstWeekday])];
NSDate *beginningOfWeek = [gregorian dateByAddingComponents:componentsToSubtract toDate:today options:0];

/*
Optional step:
beginningOfWeek now has the same hour, minute, and second as the
original date (today).
To normalize to midnight, extract the year, month, and day components
and create a new date from those components.
*/
NSDateComponents *components = [gregorian components: (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
                                            fromDate: beginningOfWeek];
beginningOfWeek = [gregorian dateFromComponents: components];
  • 3
    Note that the naming of your calendar might be a bit misleading. In most cases, the currentCalendar will be Gregorian, but it is not defined for any locale. Instead I would just name the pointer _calendar_. – ff10 Aug 30 '13 at 14:05
  • As noted by @Wolfgang, this solution is incorrect for the case where ```today``` is earlier in the week than ```firstWeekday``` (for example, firstWeekday=Monday and today=Sunday). – iOSCowboy Jan 23 '14 at 09:27
  • @JasonMoore `NSWeekdayCalendarUnit` is deprecated in favor of `NSCalendarUnitWeekday`. They were just making their naming scheme more consistent (I'm looking at you, `NSMakeRange`). The correction you mention is for `NSWeekCalendarUnit`. – DanBlakemore Jan 20 '16 at 23:54
  • True, @DanBlakemore! I've deleted my comment since yours covers it. (Yeah, I hate the backwards `NSMakeRange` too!) – Jason Moore Jan 21 '16 at 16:15
22

Why so complicated? :)

func firstDateOfWeekWithDate(date: NSDate) -> NSDate {

    var beginningOfWeek: NSDate?

    calendar.rangeOfUnit(.WeekOfYear, startDate: &beginningOfWeek, interval: nil, forDate: date)

    return beginningOfWeek!

}
Rudolf Adamkovič
  • 31,030
  • 13
  • 103
  • 118
12

The solution by naixn will produce wrong results if monday (2) is set of the firstWeekday and the date you are inspecting is sunday. In this case you will get as a result the monday of next week.

The correct way would be to calculate the subtraction components as follows:

[componentsToSubtract setDay: - ((([weekdayComponents weekday] - [gregorian firstWeekday])
                                  + 7 ) % 7)];

Assuming that you always have 7 days in a week.

Wolfgang
  • 4,865
  • 2
  • 29
  • 29
2
- (NSDate *)firstDateOfWeekWithDate:(NSDate *)date {

    NSCalendar * calendar = [NSCalendar currentCalendar];

    NSDateComponents *weekdayComponents = [calendar components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:date];

    NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:date];
    [components setDay:-((([weekdayComponents weekday] - [calendar firstWeekday]) + 5 ) % 7)];

    return [calendar dateFromComponents:components];
}
Peter Lapisu
  • 19,915
  • 16
  • 123
  • 179
2

This has already been answered, but as Wolfgang has pointed out, there is a bug in the accepted answer. If the first weekday is set as a greater date than the NSDate in question, the result will be incorrect. Here is a complete solution that works for all possible firstWeekday values (the example uses Monday).

- (NSDate *)weekOfDate:(NSDate *)originalDate
{
    NSCalendar *currentCal = [NSCalendar currentCalendar];
    [currentCal setFirstWeekday:2]; // Sun = 1, Mon = 2, etc.

    NSDateComponents *weekdayComponents = [currentCal components:NSCalendarUnitWeekday fromDate:originalDate];

    // Calculate the number of days to subtract and create NSDateComponents with them.
    NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init];
    NSInteger daysToSubtract = (([weekdayComponents weekday] - [currentCal firstWeekday]) + 7) % 7;
    [componentsToSubtract setDay:-1 * daysToSubtract];

    return [currentCal dateByAddingComponents:componentsToSubtract toDate:originalDate options:0];
}
Community
  • 1
  • 1
Kevin Marlow
  • 861
  • 7
  • 11
1

The simplest solution:

func firstDayOfWeek(date: NSDate) -> NSDate {
  let calendar = NSCalendar.currentCalendar()
  var dateComponents = calendar.components(.CalendarUnitYear | .CalendarUnitMonth | .CalendarUnitWeekOfMonth, fromDate: date)
  dateComponents.weekday = 1
  return calendar.dateFromComponents(dateComponents)!
}
Roman Ralovets
  • 131
  • 2
  • 4
  • It's off by a week for me: I implemented exactly as above and passed a date = NSDate(); it should have returned august 17, it returned august 24. – cdf1982 Aug 18 '15 at 19:34
  • You can try `.CaleandarUnitWeekOfYear | .CalendarUnitYearForWeekOfYear`, instead of `.CalendarUnitYear | .CalendarUnitMonth | .CalendarUnitWeekOfMonth`. Because a week may be shared by two month or two year. @cdf1982 , @Vassily – DawnSong Dec 14 '15 at 14:14
  • this is wrong, you are loosing information like timezone when your recompose the date. – Vassily Aug 17 '16 at 15:37
1

For Swift 4.1:

extension Date {
  var startOfWeek: Date? {
        let calendar = Calendar(identifier: .gregorian)
        return calendar.date(from: calendar.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self))
    }
}
Dare2dream
  • 76
  • 4
0

Use NSDateComponents and set the weekday to 1.

jtbandes
  • 115,675
  • 35
  • 233
  • 266