iOS Developer
iOS Developer

Reputation: 138

how to get only date and compare with only two dates in iOS?

how to get only date and compare with only two dates in iOS ? Actually i have date which is coming from server

for example : 2015-9-23

_offerEnd.text = [_offers valueForKeyPath:@"offer_valid_upto"]

And this date is compare with current date. i have tried for current date by using this.

NSDate *today = [NSDate date];
NSLog(@" total date %@",today);

unsigned int flags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay ;
NSCalendar* calendar = [NSCalendar currentCalendar];

NSDateComponents* components = [calendar components:flags fromDate:today];
NSDate* dateOnly = [calendar dateFromComponents:components];
NSLog(@" the date only o/p :%@",dateOnly);

but o/p is getting in different

the date only o/p :2015-10-12 18:30:00 +0000

please help me with this and comparison

Thanks in Advance

Upvotes: 0

Views: 754

Answers (3)

user4261201
user4261201

Reputation: 2342

Here strDate1 is the date coming from your server.

Try this:

NSString *strDate1=@"2015-09-13";

NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];

NSDate *date1=[dateFormatter dateFromString:strDate1];

NSDate *today = [NSDate date];
NSLog(@" today date %@",today);

NSString *strDate2=[dateFormatter stringFromDate:today];

NSLog(@"date 1 : %@ and date2: %@",strDate1,strDate2);

if ([strDate1 isEqualToString:strDate2])
{
    NSLog(@"Equal");
}
else
{
    NSLog(@"Not Equal");
}

NSDate *date2=[dateFormatter dateFromString:strDate2];

NSComparisonResult result = [date2 compare:date1];
switch (result) {
    case NSOrderedAscending:
        NSLog(@"Ascending");

        break;
    case NSOrderedDescending:
        NSLog(@"Descending");

        break;
    case NSOrderedSame:
        NSLog(@"Same");

        break;

    default:
        break;
}

Upvotes: 1

sargeras
sargeras

Reputation: 179

This is how you can obtain the date in the appropriate format.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
[dateFormatter setDateFormat:@"yyyy-M-d"];
NSDate *retrievedDate = [dateFormatter dateFromString:@"2015-9-23"];
NSLog(@"Date : %@", retrievedDate);

output :

Date : 2015-09-23 00:00:00 +0000

It also matters how to use this date, like converting it to string.

[dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"IST"]];
NSLog(@"Date : %@", [dateFormatter stringFromDate:retrievedDate]);

[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"EST"]];
NSLog(@"Date : %@", [dateFormatter stringFromDate:retrievedDate]);

output :

Date : 2015-09-23T05:30:00Z
Date : 2015-09-22T20:00:00Z

You can see the difference in string representation of this date.

Upvotes: 1

Jamil
Jamil

Reputation: 2999

Try with this.

NSDate *nowDate = [NSDate date];
NSDate *dateOnly = [self getDateFrom:@"2015-9-23"];//[NSDate dateWithTimeIntervalSinceReferenceDate:3600];//second
NSComparisonResult result = [dateOnly compare:nowDate];
switch (result) {
    case NSOrderedAscending:
        NSLog(@"Past");
        break;
    case NSOrderedDescending:
        NSLog(@"Future");
        break;
    case NSOrderedSame:

        break;

    default:
        break;
}

convert date method from string

-(NSDate*)getDateFrom:(NSString*)dateString{
// 2015-9-23
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd";
[formatter setDateFormat:@"yyyy-MM-dd"];
NSDate *date = [formatter dateFromString:dateString];
NSLog(@"date: %@",date);
return date;
}

Upvotes: 1

Related Questions