0

is there any way to create events for NSDate ? here is my code

    NSDate *date = [NSDate date];       
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateFormat:@"MMMM d, yyyy"];
    NSString *dateStr = [dateFormatter stringFromDate:date]; 
    myDate.text = dateStr;

for example if date = 12 FEB ; myDate .text = @"Mother Day";

something like this

iOS.Lover
  • 5,923
  • 21
  • 90
  • 162

2 Answers2

1

sure it is. You have to split the date into day and month using NSDateComponents

You could write a method like this:

- (BOOL)date:(NSDate *)date isSameAsDay:(NSInteger)day andMonth:(NSInteger)month {
    NSUInteger dateFlags = NSDayCalendarUnit | NSMonthCalendarUnit;
    NSDateComponents *components = [[NSCalendar currentCalendar] components:dateFlags fromDate:date];
    if ([components day] == day && [components month] == month) {
        return YES;
    }
    return NO;
}

and you would use it like this

if ([self date:[NSDate date] isSameAsDay:12 andMonth:2]) {
   myDate.text = @"Mother Day";
}
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
1

This should do it:

    NSDate *today = [NSDate date]; // Get ref to todays date
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *weekdayComponents =
    [gregorian components:(NSWeekdayOrdinalCalendarUnit | NSWeekdayCalendarUnit | NSMonthCalendarUnit) fromDate:today];
    NSInteger weekday = [weekdayComponents weekday]; // Sun == 1, Mon == 2, Tue...
    NSInteger weekdayOrdinal = [weekdayComponents weekdayOrdinal]; // First weekday month == 1 etc...
    NSInteger month = [weekdayComponents month];
    NSLog (@"%i %i %i", weekday, weekdayOrdinal, month);

    // Mothers day is every second Sunday of May so weekday == 1, weekdayOrdinal == 2, month == 5
    if ((weekday == 1) && (weekdayOrdinal == 2) && (month == 5)) {
        NSLog (@"It's mothers day!");
    }
    [gregorian release];
Rog
  • 18,602
  • 6
  • 76
  • 97