如何在ios中将服务器的日期与[NSDate date]进行比较?

时间:2014-01-03 06:19:07

标签: ios iphone nsdateformatter

我在 01/02/2014 16:43:00 AM 格式中从服务器获取日期字符串,该格式具有 GMT timeZone

我要将该日期与实时流媒体的当前日期进行比较。如果当前日期小于该服务器日期,我将显示一些警告,显示正确的实时开始时间。 但我无法比较它们

如何将该服务器日期字符串转换为日期格式以及如何将该日期与当前日期进行比较?

我正在使用以下代码:

NSString *DateFromServer = video.Eventutcstart;
NSDateFormatter * dateFormatter1 = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
NSString *tzName = [timeZone name];
[dateFormatter1 setTimeZone:[NSTimeZone timeZoneWithName:tzName]];
[dateFormatter1 setDateFormat:@"MM/dd/yyyy HH:mm:ss a"];
NSString *str = [dateFormatter1 stringFromDate:[NSDate date]];
NSDate *currentDate = [dateFormatter1 dateFromString:str];
NSDate *serverDate = [dateFormatter1 dateFromString: DateFromServer];
NSLog(@"currentDate %@", currentDate);
NSLog(@"serverDate %@", serverDate);
NSComparisonResult result;

result = [currentDate compare:serverDate]; 

if(result == NSOrderedDescending)
{
    NSLog(@"alert message");
}

输出:

2014-01-03 11:30:25.346 VMS[955:70b] currentDate 2014-01-02 19:00:23 +0000

2014-01-03 11:30:32.088 VMS[955:70b] serverDate 2014-01-02 19:00:46 +0000

请帮我解决这个问题....

4 个答案:

答案 0 :(得分:0)

请使用以下代码。您没有正确设置时区:

NSString *DateFromServer = video.Eventutcstart;
NSDateFormatter * dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setTimeZone:[NSTimeZone localTimeZone]];
[dateFormatter1 setDateFormat:@"MM/dd/yyyy HH:mm:ss a"];
NSString *str = [dateFormatter1 stringFromDate:[NSDate date]];
NSDate *currentDate = [dateFormatter1 dateFromString:str];
NSDate *serverDate = [dateFormatter1 dateFromString: DateFromServer];
NSLog(@"currentDate %@", currentDate);
NSLog(@"serverDate %@", serverDate);
NSComparisonResult result;

result = [currentDate compare:serverDate];

if(result == NSOrderedDescending)
{
    NSLog(@"alert message");
}

答案 1 :(得分:0)

仅限日期比较, 您有两个正确获得的日期,因此可以使用:

int days1 = [currentDate timeIntervalSince1970] / 86400;
int days2 = [serverDate timeIntervalSince1970] / 86400;
if(days1==days2)
 {
   //do something;
 }
else
 {
   //do something
 }

以上代码不需要考虑时间,只比较年/月/日

用于NSdate时间比较

unsigned int flags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSCalendar* calendar = [NSCalendar currentCalendar];

NSDateComponents* component1 = [calendar components:flags fromDate:currentDate];
NSDateComponents* component2 = [calendar components:flags fromDate:serverDate];


NSDate* currentDateTimeOnly = [calendar dateFromComponents:component1];
NSDate* serverDateTimeOnly = [calendar dateFromComponents:component2];

NSComparisonResult result;

result = [currentDateTimeOnly compare:serverDateTimeOnly];

if(result == NSOrderedDescending)
{
    NSLog(@"alert message");
}

答案 2 :(得分:0)

试试这段代码

if(result == NSOrderedAscending)
{
    NSLog(@"alert message");
}

而不是这个

if(result == NSOrderedDescending)
{
    NSLog(@"alert message");
}

如果你想用两个日期检查两个约束,那么你可以使用下面的代码。

if(result == NSOrderedAscending){
    NSLog(@"serverDate is in the future");
}else if(result == NSOrderedDescending){
    NSLog(@"serverDate is in the past");
}else{
    NSLog(@"Both dates are the same");
}

答案 3 :(得分:0)

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


NSUserDefaults *def=[NSUserDefaults standardUserDefaults];

NSString *a1=[def objectForKey:@"Currentdate"];
NSDate *now = [dateFormat dateFromString:a1];

NSDate * currentdate = [NSDate date];
NSString *nowstr=[dateFormat stringFromDate:currentdate];
    NSDate *todaydate=[dateFormat dateFromString:nowstr];

NSLog(@"Now is %@ and current date is %@",[def objectForKey:@"Currentdate"],nowstr);
NSComparisonResult result = [todaydate compare:now];

switch (result)
{
    case NSOrderedAscending:
        dateiswhich=-1;
        NSLog(@"%@ is in future from %@", currentdate, now);
        break;
    case NSOrderedDescending:
        dateiswhich=1;
        NSLog(@"%@ is in past from %@", currentdate, now);
        break;
    case NSOrderedSame: dateiswhich=-1;

        break;
    default: NSLog(@"erorr dates %@, %@", currentdate, now); break;
}

使用上面的代码可能对您有用