如何只获得日期并与iOS中的两个日期进行比较?

时间:2015-10-13 10:00:32

标签: ios objective-c date

如何获取日期并仅与iOS中的两个日期进行比较? 实际上我有来自服务器的日期

例如:2015-9-23

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

此日期与当前日期进行比较。 我已经尝试过使用此功能。

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);

但是o / p正在进入不同的

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

请帮我解决这个问题和比较

先谢谢

3 个答案:

答案 0 :(得分:1)

此处strDate1是来自您服务器的日期。

试试这个:

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;
}

答案 1 :(得分:1)

试试这个。

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;
}

从字符串

转换日期方法
-(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;
}

答案 2 :(得分:1)

这是您以适当格式获取日期的方法。

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);

输出:

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

如何使用此日期也很重要,例如将其转换为字符串。

[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]);

输出:

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

您可以看到此日期的字符串表示形式的差异。