2

I got to two date string in 24 Hours format how to compare ? For example

The current date :2013-06-27 13:51
The server date :2013-06-27 11:51

The current date cannot greater than the server date.Please help

Here is the datepicker

 NSDateFormatter *datePickerFormat = [[NSDateFormatter alloc] init];
 [datePickerFormat setDateFormat:@"yyyy-MM-dd HH:mm"];
 self.myCurrentDate = [datePickerFormat stringFromDate:self.myDatePicker.date];

 NSDate *myDate = [datePickerFormat dateFromString:self.myCurrentDate];

I not able to convert it to 24 hours format, and final datetime show is incorrect.

Jason lau
  • 111
  • 2
  • 9
  • Please be more specific as to if you are trying to compare an `NSDate` or `NSString`. – WrightsCS Jun 27 '13 at 04:21
  • Now that you have updated your question with more details, the question becomes - why compare date strings? Store the two `NSDate` objects from the picker(s). Then compare the two `NSDate` objects. Only use strings to display the dates to the user. – rmaddy Jun 27 '13 at 04:26
  • Hi maddy,Thx for your reply, Yes I having problem when converting datepicker to NSDate. – Jason lau Jun 27 '13 at 04:30
  • Huh? What do you mean by "convert datepicker to NSDate"? Just assign the date picker's `date` property to an `NSDate` property (instead of an `NSString` property). Do all of your work with the `NSDate` objects. The only time you should convert the dates to strings is to display them to the user. – rmaddy Jun 27 '13 at 04:32
  • Use this instead of your last line of code: `NSDate *myDate = self.myDatePicker.date;` and then compare it to the date returned by your server in the same manner as the duplicate question pointed out by @TheTiger. You don't need any of the other three lines of code for this either. – lnafziger Jun 27 '13 at 04:45

5 Answers5

5

Try this,

//  The current date :2013-06-27 13:51
//  The server date :2013-06-27 11:51

NSString *strCurrentDate = @"2013-06-27 13:51";
NSString *strServerDate =@"2013-06-27 11:51";
NSDateFormatter *datePickerFormat = [[NSDateFormatter alloc] init];
[datePickerFormat setDateFormat:@"yyyy-MM-dd HH:mm"];
NSDate *currentDate = [datePickerFormat dateFromString:strCurrentDate];
NSDate *serverDate = [datePickerFormat dateFromString:strServerDate];

NSLog(@"date %@",currentDate);
NSLog(@"date %@",serverDate);

NSComparisonResult result;

result = [currentDate compare:serverDate]; // comparing two dates

if(result == NSOrderedAscending)
    NSLog(@"current date is less");
else if(result == NSOrderedDescending)
    NSLog(@"server date is less");
else if(result == NSOrderedSame)
    NSLog(@"Both dates are same");
else
    NSLog(@"Date cannot be compared");
Ravindhiran
  • 5,304
  • 9
  • 50
  • 82
  • Hi Ravichiran,I get the following output date 2013-06-27 05:51:00 +0000 date 2013-06-27 03:51:00 +0000 server date is less I got one question why after convert to NSDate it become 05:51:00 +0000 ? – Jason lau Jun 27 '13 at 06:12
0

Try this class utility:

.h

#import <Foundation/Foundation.h>

@interface NSDate (Compare)
-(BOOL) isLaterThanOrEqualTo:(NSDate*)date;
-(BOOL) isEarlierThanOrEqualTo:(NSDate*)date;
-(BOOL) isLaterThan:(NSDate*)date;
-(BOOL) isEarlierThan:(NSDate*)date;
@end

.m

#import "NSDate+Compare.h"

@implementation NSDate (Compare)

-(BOOL) isLaterThanOrEqualTo:(NSDate*)date {
    return !([self compare:date] == NSOrderedAscending);
}
-(BOOL) isEarlierThanOrEqualTo:(NSDate*)date {
    return !([self compare:date] == NSOrderedDescending);
}
-(BOOL) isLaterThan:(NSDate*)date {
    return ([self compare:date] == NSOrderedDescending);
}
-(BOOL) isEarlierThan:(NSDate*)date {
    return ([self compare:date] == NSOrderedAscending);
}

@end
WrightsCS
  • 50,551
  • 22
  • 134
  • 186
  • Clearly says `NSDate`. I use these successfully in one of my apps. – WrightsCS Jun 27 '13 at 04:10
  • Read the `NSDate` documentation, specifically about `NSComparisonResult`. `-(NSComparisonResult)compare:(NSDate*)other;` https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html#//apple_ref/occ/instm/NSDate/compare: – WrightsCS Jun 27 '13 at 04:13
  • He said "date string", not specifically **`NSString`**, you only assume NSString because you see the word string. With 1 reputation point and 2 questions asked, this question should be more specific. – WrightsCS Jun 27 '13 at 04:15
  • You are both wrong, you can print an NSDate by using an NSString argument in NSLog, for example `NSLog(@"Todays date: %@", [NSDate date]);`. – WrightsCS Jun 27 '13 at 04:19
  • If you want to get technical, then you can always convert the `NSString` to `NSDate` and compare. – WrightsCS Jun 27 '13 at 04:22
  • And, if you want to get technical, printing that NSLog calls the NSDate's description method in order to get an NSString. However, none of this discussion helps the OP with his question..... – lnafziger Jun 27 '13 at 04:39
0

Try this code for any comparison between dates... You should not compare date in the form of string. Compare the dates before conversion to string. Convert the self.serverDate into date format using dateFromString function of the formatter by specifying the exact date format as that of the dateString. Then compare the dates using following function.

NSDate *today = [NSDate date]; // current date
NSDate *newDate = self.serverDate; // other date 

NSComparisonResult result; 

result = [today compare:newDate]; // comparing two dates

if(result == NSOrderedAscending)
    NSLog(@"today is less");
else if(result == NSOrderedDescending)
    NSLog(@"newDate is less");
else if(result == NSOrderedSame)
    NSLog(@"Both dates are same");
else
    NSLog(@"Date cannot be compared");
prince
  • 854
  • 2
  • 9
  • 36
0
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
                [dateFormatter setDateFormat:@"yyyy-MM-dd"];
                NSDate *date1 = [dateFormatter dateFromString:TodatDate];

                NSDateFormatter *dateFormatte = [[[NSDateFormatter alloc] init] autorelease];
                [dateFormatte setDateFormat:@"yyyy-MM-dd"];
                NSDate *date2 = [dateFormatte dateFromString:ServerDate];

                unsigned int unitFlags = NSDayCalendarUnit;

                NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
                NSDateComponents *comps = [gregorian components:unitFlags fromDate:date1  toDate:date2  options:0];

                int days = [comps day];
                NSLog(@"%d",days);
Bhavesh Nayi
  • 3,626
  • 1
  • 27
  • 42
  • Bhavesh Nai, I want convert in 24 Hours format – Jason lau Jun 27 '13 at 04:48
  • means 1 second, 1 minute, 1 hours – Bhavesh Nayi Jun 27 '13 at 04:52
  • @Jasonlau Try This unsigned int unitFlags = NSDayCalendarUnit; or unsigned int unitFlags = NSHourCalendarUnit; or unsigned int unitFlags = NSMinuteCalendarUnit; or unsigned int unitFlags = NSYearCalendarUnit; or unsigned int unitFlags = NSMonthCalendarUnit; or – Bhavesh Nayi Jun 27 '13 at 04:57
0

You cannot compare two date string initially you have to convert that string to two date by using the following code

//  The current date :2013-06-27 13:51
//  The server date :2013-06-27 11:51

    NSString *serverDate = @"2013-06-27 13:51";
        NSDateFormatter *datePickerFormat = [[NSDateFormatter alloc] init];
        [datePickerFormat setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
        [datePickerFormat setDateFormat:@"yyyy-MM-dd HH:mm"];
        NSDate *convertedDate = [datePickerFormat dateFromString:serverDate];
        NSLog(@"date %@",convertedDate);

after conversion you can compare using the following code as said by @prince

NSDate *today = [NSDate date]; // current date
NSDate *newDate = self.serverDate; // other date 

NSComparisonResult result; 

result = [today compare:newDate]; // comparing two dates

if(result == NSOrderedAscending)
    NSLog(@"today is less");
else if(result == NSOrderedDescending)
    NSLog(@"newDate is less");
else if(result == NSOrderedSame)
    NSLog(@"Both dates are same");
else
    NSLog(@"Date cannot be compared");
Vinodh
  • 5,262
  • 4
  • 38
  • 68
  • Actually, if you have two date strings in the format "yyyy-MM-dd HH:mm" then you can safely compare the two strings. They will compare properly. There would be no need to convert such strings to `NSDate` objects. But this is all moot since the OP actually already has two `NSDate` objects to begin with. – rmaddy Jun 27 '13 at 04:57
  • Hi Soup boy, after the convert to NSDate from NSString the Time is incorrect already.For example 10.00 PM it show 12.00 (24 hours format). – Jason lau Jun 27 '13 at 05:03
  • i added timezone [datePickerFormat setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]]; , have you used my code @Jasonlau – Vinodh Jun 27 '13 at 05:37
  • @rmaddy : Sting comparison result will not give give time comaprison resut – Vinodh Jun 27 '13 at 05:37
  • @SoupBoy Yes it will if the date strings have the format I indicated. Calling `compare:` on the two date string will give the same results as calling `compare:` on two `NSDate` objects for the same date/time values. But to be clear, I don't recommend using strings for this. – rmaddy Jun 27 '13 at 05:41
  • @SoupBoy,i used this [datePickerFormat setTimeZone:[NSTimeZone timeZoneWithName:@"Malaysia/Kuala Lumpur"]]; Why still display the same ? – Jason lau Jun 27 '13 at 06:27
  • check the apple docs and use the name , may be name conflict , or set with UTC+your time zone – Vinodh Jun 27 '13 at 06:41