2011-05-03 4 views
0

Я вычисляю разницу между двумя датами в моем testapp. Он отлично работает, если я не меняю месяц, но когда я меняю месяц, он не работает. Пожалуйста, помогите мне здесь код, который я использую: -Как я могу получить разницу по дате?

NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease]; 
//[df setDateStyle:NSDateFormatterMediumStyle]; 
df.dateFormat = @"YYYY-MM-DD"; 

NSDate *date1 = [df dateFromString:@"2011-04-30"]; 
NSDate *date2 = [df dateFromString:@"2011-05-03"]; 

NSLog(@"date1=%@",date1); 
NSLog(@"date2=%@",date2); 
NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1]; 

int numberOfDays = secondsBetween/86400; 

NSLog(@"There are %d days in between the two dates.", numberOfDays); 

мощность -27. Я в замешательстве.

пожалуйста, кто поможет мне решить эту проблему ..

ответ

2

Попробуйте

NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease]; 
     //[df setDateStyle:NSDateFormatterMediumStyle]; 
     //df.dateFormat = @"yyyy-mm-dd"; 
    [df setDateFormat:@"yyyy-MM-dd"]; 
    NSDate *date1 = [df dateFromString:@"2011-04-30"]; 
    NSDate *date2 = [df dateFromString:@"2011-05-03"]; 

NSLog(@"date1=%@",date1); 
NSLog(@"date2=%@",date2); 
NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1]; 

int numberOfDays = secondsBetween/86400; 

NSLog(@"There are %d days in between the two dates.", numberOfDays); 

Настройка часового пояса, чтобы получить правильные даты ...

1

Вы должны сказать NSDate формат вы используете:

NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
df.dateFormat = @"yyyy'-'MM'-'dd"; 
NSLocale *local = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; // Your locale 
df.locale = local; 
[local release]; 
df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; // Your timezone 
NSDate *date1 = [df dateFromString:@"2011-04-30"]; 
NSDate *date2 = [df dateFromString:@"2011-05-03"]; 
[df release]; 
0

Изменить

NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1]; 

в

NSTimeInterval secondsBetween = [date1 timeIntervalSinceDate:date2]; 
3

Вы также можете попробовать этот метод, из которого вы можете получить любой вид разницы с двух дня ...

-(NSString *)timeSinceTimestamp:(NSString *)seconds 
{ 
  double seconds2 = [seconds doubleValue]; 

  NSDate *date = [NSDate dateWithTimeIntervalSince1970:seconds2]; 

  NSDate *now = [NSDate date]; 

  double start = [date timeIntervalSince1970]; 

  double end = [now timeIntervalSince1970]; 

  double difference = (end - start)/1000; 

  difference = round(difference); 

  int minutes = difference/60; 

  int hours = minutes/60; 

  int days = hours/24; 

  int weeks = days/7; 

  int months = weeks/5; 

  int years = months/12; 

  NSString *string; 

  

    if(difference < 60) 
    { 
       string = [NSString stringWithFormat:@"%i seconds ago",difference]; 

    } 

    else if (minutes > 1 && minutes < 60) 
    { 
      string = [NSString stringWithFormat:@"%i minutes ago",minutes]; 
    } 

    else if (hours > 1 && hours < 24) 
    { 
     string = [NSString stringWithFormat:@"%i hours ago",hours]; 
    } 

    else if (days > 1 && days < 7) 
    { 
        string = [NSString stringWithFormat:@"%i days ago",days]; 
    } 



    else if (weeks > 1 && weeks < 5) 

    { 

      string = [NSString stringWithFormat:@"%i weeks ago",weeks]; 

  } 
    else if (months > 1  && months < 12) 

    { 
      string = [NSString stringWithFormat:@"%i months ago",months]; 


  } 
    else if (years > 1 && years < 12) 

    { 

      
    string = [NSString stringWithFormat:@"%i years ago",years]; 


    } 

    return string; 

} 
Смежные вопросы