Sunday, July 17, 2016

Compare Two dates- ios


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    
    NSDate *todayDate = [NSDate date]; // get today date
    NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init]; // here we create      NSDateFormatter object for change the Format of date..
    [dateFormatter2 setDateFormat:@"yyyy-MM-dd hh:mm:ss Z"]; //Here we can set the format which we need
    NSString *convertedDateString = [dateFormatter2 stringFromDate:todayDate];// here convert date in
  NSLog(@"Today formatted date is %@",convertedDateString);
    
    
    
    
    
    NSString *getTokon_Time1 = @"2016-07-20 03:19:05 +0000";
   // NSString *getTokon_Time2 = @"2016-07-31 03:18:05 +0000";
    NSDateFormatter *dateFormatter=[NSDateFormatter new];
    [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss Z"];
    NSDate *tokonExpireDate1=[dateFormatter dateFromString:getTokon_Time1];
    NSDate *tokonExpireDate2=[dateFormatter dateFromString:convertedDateString];
    BOOL isTokonValid = [self dateComparision:tokonExpireDate1 andDate2:tokonExpireDate2];
    
    
    // Do any additional setup after loading the view.
}

-(BOOL)dateComparision:(NSDate*)date1 andDate2:(NSDate*)date2{
    
    BOOL isTokonValid;
    
    if ([date1 compare:date2] == NSOrderedDescending) {
        NSLog(@"date1 is later than date2");
        
        
        
        isTokonValid = YES;
        
        
        
    } else if ([date1 compare:date2] == NSOrderedAscending) {
        NSLog(@"date1 is earlier than date2");
        isTokonValid = NO;
        
        
      
    } else {
        NSLog(@"dates are the same");
        isTokonValid = NO;
        
    }
    
    return isTokonValid;


}

No comments:

Post a Comment