tags:

views:

59

answers:

1

How can I check if an NSDate is a specific day of the week, like a Monday, Tuesday, Wednesday, and so on?

+4  A: 

From memory (I don't have an ObjC environment handy at the moment):

int yourDOW = [[[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit
    fromDate:yourDate] weekday];
if (yourDOW == 2) { ... }     // Sun = 1, Sat = 7

In other words:

  • Get the user's current calendar.
  • Get the weekday unit based on the given date.
  • Get the week day from that.
  • Compare it with what you want.
paxdiablo
Your memory is quite well.
Hankweb
Would 1 be Sunday, or would 0 be Sunday?
Hankweb
It depends on your calendar, see http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSDateComponents_Class/Reference/Reference.html for details: Weekday units are the numbers 1 through n, where n is the number of days in the week. For example, in the Gregorian calendar, n is 7 and Sunday is represented by 1.
paxdiablo