-4

how can i check if the date entered by the date picker is before a certain day which is stored as a variable, the year does not matter.

for example, i need to check the date which is picked on the date picker to let me know if its before the 5th of January or after the 5th of January. the result should trigger an IF function if its true.

NSDate * dateOne = [NSDate date];
NSDate *dt1 = NSDate date];

if([dateOne compare:dateTwo] == NSOrderedAscending) {
    NSLog(@"TRUE");
}
Mugs
  • 129
  • 10

2 Answers2

4

What you want to do is split up the the currect date in to NSDateComponents.

NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDateComponents *components = [currentCalendar components:NSDayCalendarUnit | NSMonthCalendarUnit fromDate:[NSDate date]];

if (components.month < 2 || (components.month == 2 && components.day < 5) ){
    // Before the 5th of february.
}
rckoenes
  • 69,092
  • 8
  • 134
  • 166
  • Even 3Feb 2050 will be before 5th Feb? isn't an interesting question? – Anoop Vaidya Mar 29 '13 at 09:27
  • You are not checking on the year so yes. In my example I'm only checking if the month is lower then 2 and day is lower then 5 thus the date is before the 5th of february. – rckoenes Mar 29 '13 at 09:29
-1

you can get just date and month of selected date like bellow..

NSInteger sDay = [[[yourDatePicker date] stringFromDateWithFormat:@"dd"] integerValue];
NSInteger sMonth = [[[yourDatePicker date] stringFromDateWithFormat:@"MM"] integerValue];
if ( sMonth < 2 || (sMonth  == 2 && sDay < 5) ) {
        NSLog(@"yes its further");
}

And below is a method which you can use for convert any date with any date format.

-(NSString*)stringFromDateWithFormat:(NSString*)strDateFormat{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
     //[formatter setTimeZone:[NSTimeZone systemTimeZone]];
    [formatter setDateFormat:strDateFormat];
    NSString *str = [formatter stringFromDate:self];
    [formatter release];
    return str;
}
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • Even 3Feb 2050 will be before 5th Feb? isn't an interesting question? – Anoop Vaidya Mar 29 '13 at 09:26
  • 1
    Using `NSDateFormatter` is a bit slow also this is what `NSDateComponents` is ment for. – rckoenes Mar 29 '13 at 09:31
  • yes mate first i think about that and first i post this above simple logic and after when came for post new code with NSDateFormatter then i see your answer.. but its also working right dude... :) – Paras Joshi Mar 29 '13 at 09:34
  • Your code will not work with any date past the 5th of january. since the day will be lager then 5 but the month will not be. So `6-jan-2013` will not pass your test. – rckoenes Mar 29 '13 at 09:45
  • 1
    worked great, but had to check the month first and then the date – Mugs Mar 29 '13 at 09:56
  • @rckoenes try my code it will worked dude.. i check it out and also if its right then upvote me if its right.. :) – Paras Joshi Mar 29 '13 at 09:58
  • Your code still contains the bug, test it out with any date in january after january 5th. I'm trying to help out. – rckoenes Mar 29 '13 at 09:59
  • @rckoenes thnx dude i know whats wrong here now i edit test my latest code... :) – Paras Joshi Mar 29 '13 at 10:02
  • @Mugs use this latest code with perfact condition which give you perfact output.. :) – Paras Joshi Mar 29 '13 at 10:03
  • @ParasJoshi Still not working for any dates after the 7th of January. You need to split the month day check like I did my answer – rckoenes Mar 29 '13 at 10:12
  • @rckoenes now i see your code and also implement it see... :) – Paras Joshi Mar 29 '13 at 10:15
  • It needs to been `<5` not smaller then 6, since the date has to be before the 5th. Als now it's just my answer only then with the slow `NSDateFormatter` – rckoenes Mar 29 '13 at 10:20
  • @rckoenes its include 5th feb also in that condition i know about that its print that further in even 5th feb is selected and now its working... – Paras Joshi Mar 29 '13 at 10:22
  • @rckoenes now its working well i test it now with different date like 2 jan and 17 jan and 3 feb,etc.. – Paras Joshi Mar 29 '13 at 10:24
  • No visible @interface for 'NSDate' declares the selector 'date' – Mugs Mar 29 '13 at 10:27