4

How to get first sunday or nth sunday or monday (any day) date in Objective C coding.

For example :- I just want to show date of friendship day in my app every year.

But, friendship comes 1st Sunday of Aug. So, date will change every year. here I need to find what is the date of 1st Sunday in Aug every year.

is there any logic to find the date of nth Sunday.

Naga Harish M
  • 2,779
  • 2
  • 31
  • 45

5 Answers5

8

You need to create a NSDateComponents object and set the appropriate values, In You example you would do:

NSDateComponents *dateComponents = [[NSDateComponents alloc] init];

dateComponents.year = 2011; // set the current year or whatever year you want here
dateComponents.month = 8;
dateComponents.weekday = 1; // sunday is 1, monday is 2, ...
dateComponents.weekdayOrdinal = 1; // this means, the first of whatever weekday you specified

To convert this into a NSDate-object, you just do:

//you may want to use another calendar object here
NSDate *myDate = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
[dateComponents release]; //don't forget memory management ;)
David Lari
  • 943
  • 11
  • 21
Ahti
  • 1,420
  • 1
  • 11
  • 20
  • Small query.. I want to find 2nd Sunday from last week.. is there any way to find by changing weekdayOrdinal to -ve value `dateComponents.weekdayOrdinal = -2;` – Naga Harish M Oct 20 '11 at 14:54
4

You want to use NSDateComponents in combination with NSCalendar's dateFromComponents: method:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *c = [[NSDateComponents alloc] init];
[c setYear:2012];
[c setWeekday:1]; // Sunday
[c setWeekdayOrdinal:3]; // The 3rd Sunday

for (int i = 1; i <= 12; i++) {
    [c setMonth:i];
    NSDate *date = [gregorian dateFromComponents:c];
    NSLog(@"Date[%i]: %@",i,date);
}

[c release];
[gregorian release];
Benjie
  • 7,701
  • 5
  • 29
  • 44
  • You code is very useful to get every month 3rd sunday. Can you please help, one more query How to get total number Sundays in a month? – Naga Harish M Oct 20 '11 at 15:02
  • 1
    There might be a better way, but you can use this `NSCalendar` method: - (NSRange)rangeOfUnit:(NSCalendarUnit)smaller inUnit:(NSCalendarUnit)larger forDate:(NSDate *)date to get the number of days in a month, then subtract the date of the first Sunday and divide by 7 (and `floor` the result). You'll need to check for off-by-one issues with this suggestion. – Benjie Oct 20 '11 at 15:18
1

Here's an example. You can find more information in the Date and Time Programming Guide.

To find the first Sunday in August:

NSDateComponents *components = [[NSDateComponents alloc] init];
[components setWeekday:1]; // Sunday
[components setWeekdayOrdinal:1]; // The first Sunday in the month
[components setMonth:8]; // August
[components setYear:2011];
NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:components];
jluckyiv
  • 3,691
  • 3
  • 22
  • 15
0

you check the first day of the month.

unsigned units = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:units fromDate:date];

Get the day using [components day]; <-1 for Sunday.After this it's simple.

0

You'll want to review the NSCalendar class here.

Of specific interest will be the method - (NSUInteger)ordinalityOfUnit:(NSCalendarUnit)smaller inUnit:(NSCalendarUnit)larger forDate:(NSDate *)date

logancautrell
  • 8,762
  • 3
  • 39
  • 50