close

Minus 1 Day from Week, 5 Days from Month and 1 Month from Year Time Interval Using NSDateFormatter

QuestionsCategory: Objective CMinus 1 Day from Week, 5 Days from Month and 1 Month from Year Time Interval Using NSDateFormatter
Ahmad asked 1 year ago

I want to check date after I minus 1 day from week and 5 days from month and 1 month from year using NSDateFormatter.

1 Answers
Chaudhary Talha Staff answered 1 year ago
-(BOOL)weeklyCheck:(NSString *)dayLabel{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"EEEE"];
    
    NSDate *now = [NSDate date];
    NSDate *newDate1 = [now dateByAddingTimeInterval:60*60*24*-1];
    
    NSString *currentDay = [dateFormatter stringFromDate:newDate1];
    
    NSLog(@"%@, %@",currentDay,dayLabel);
    
    if ([dayLabel isEqualToString:currentDay]) {
        NSLog(@"Yup Working");
        return YES;
    }
    else return NO;
    
}

 

-(BOOL)monthlyCheck:(NSString *)date{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd"];
    
    NSDate *now = [NSDate date];
    NSDate *newDate1 = [now dateByAddingTimeInterval:60*60*24*-5];
    
    NSString *currentDate = [dateFormatter stringFromDate:newDate1];
    
    NSLog(@"%@, %@",currentDate,date);
    
    int cd = [currentDate intValue];
    int d = [date intValue];
    
    if (cd==d) {
        NSLog(@"Yup 2 down");
        return YES;
    }
    else return NO;
}

 

-(BOOL)yearlyCheck:(NSString *)day andMonth:(NSString *)month{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setDateFormat:@"dd/MMM"];
    
    NSDate *date = [NSDate date];
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    [dateComponents setMonth:-1];
    date = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:date options:0];
    
    NSString *currentDate = [dateFormatter stringFromDate:date];
    NSArray *CurrentArr = [currentDate componentsSeparatedByString:@"/"];
    
    NSString *newDay = [CurrentArr objectAtIndex:0];
    NSString *newMonth = [CurrentArr objectAtIndex:1];
    
    int d = [day intValue];
    int nd = [newDay intValue];
    
    NSLog(@"%@ %@, %@ %@",newDay,newMonth,day,month);
    
    if (d==nd&&[month isEqualToString:newMonth]) {
        NSLog(@"Ofcourse it works");
        return YES;
    }
    else return NO;

}