1

I have two datepickers that gives me a startTime and an endTime. according to this code:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.timeZone=[NSTimeZone defaultTimeZone];
dateFormatter.timeStyle=NSDateFormatterShortStyle;
dateFormatter.dateStyle=NSDateFormatterShortStyle;
NSString *dateTimeString=[dateFormatter stringFromDate:startTime.date];
NSLog(@"Start time is %@",dateTimeString);


NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc]init];
dateFormatter2.timeZone=[NSTimeZone defaultTimeZone];
dateFormatter2.timeStyle=NSDateFormatterShortStyle;
dateFormatter2.dateStyle=NSDateFormatterShortStyle;
NSString *dateTimeString2=[dateFormatter2 stringFromDate:endTime.date];
NSLog(@"End time is %@",dateTimeString2);

in my .h

@property (weak, nonatomic) IBOutlet UIDatePicker *startTime;
@property (weak, nonatomic) IBOutlet UIDatePicker *endTime;

and I have this code that let me compare between the giving time and current time. What is the best way to test it as I need to you use it in the background.

if ([[NSDate date] isEqualToDate:startTime.date]) {
NSLog(@"currentDate is equal to startTime"); 
}

if ([[NSDate date] isEqualToDate:endTime.date]) {
NSLog(@"currentDate is equal to endTime"); 
}

I need to choose between that code or this one. so would NSLog work ok here?

- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate


if([startTime.date timeIntervalSinceDate:[NSDate date]] > 0)
{
//start time greater than today
}

else if([startTime.date timeIntervalSinceDate:[NSDate date]] < 0)
{
//start time less than today
}

else
{
//both dates are equal
}

Thanks

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
user1949873
  • 460
  • 7
  • 17
  • have a look here: http://stackoverflow.com/questions/15890205/if-statement-with-dates/15890470#15890470 – vikingosegundo Apr 09 '13 at 23:27
  • It's somewhat related but I didn't really understand it! I'm only wondering if printing an NSLog would be an ideal test for such function or there is actually a better idea to deal with my request? – user1949873 Apr 10 '13 at 02:43
  • other question: wy wouldnt NSLog work there? – vikingosegundo Apr 10 '13 at 08:24
  • @vikingosegundo Because I need to use it the background and when I test with NSLog it returns nothing! I'm starting to think that my way of declaring NSDate in ViewController and calling it in AppDelegate is the issue! do you have any idea? – user1949873 Apr 10 '13 at 13:20
  • if nslog prints out nothing, it just can be 2 reason: 1) the statement is never reached 2) the printed object does not exist. is nil. – vikingosegundo Apr 10 '13 at 15:31

2 Answers2

6
NSDate *date1;
NSDate *date2;

Then the following comparison will tell which is earlier/later/same:

 if ([date1 compare:date2] == NSOrderedDescending) {
 NSLog(@"date1 is later than date2");        

 } else if ([date1 compare:date2] == NSOrderedAscending) {
  NSLog(@"date1 is earlier than date2");

 } else {
NSLog(@"dates are the same");
 }
Madhu
  • 994
  • 1
  • 12
  • 33
1

You Can use NSComparator to compare two objects value

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSDate *strDate;
NSDate *endDate;
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
strDate = [formatter dateFromString:@"2012-12-07 08:43:31"];
endDate = [formatter dateFromString:@"2012-12-07 08:43:30"];

NSLog(@"%d",[strDate compare:endDate]);
if([strDate compare:endDate] < 0)
    NSLog(@"Enddate bigger than serdate");
else if([strDate compare:endDate] > 0)
    NSLog(@"strdate bigger than enddate");
else
    NSLog(@"Both Are Identicle");
Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57
  • Do you have an idea on how to make the variables global? so I can take it from ViewController to the Method didEnterBackground in appdelgate? – user1949873 Apr 10 '13 at 13:17