从NSDate倒数天数小时的问题

时间:2013-07-23 09:43:50

标签: iphone ios nsdate nstimer nstimeinterval

我正在努力更新UILabel倒数,因为我使用下面的代码

- (void)updateLabel {

    // convert date string to date then set to a label
    NSDateFormatter *dateStringParser = [[NSDateFormatter alloc] init];
    [dateStringParser setDateFormat:@"MM/dd/yyyy"];

    NSLog(@"date from update label %@", _selectedBirthdate);
    //OUTPUT  date from update label  07/23/2013

    NSDate *date = [dateStringParser dateFromString:_selectedBirthdate];

    NSLog(@"date from update label %@", date);

    NSTimeInterval timeInterval = [date timeIntervalSinceNow]; ///< Assuming this is in the future for now.

    NSString *stringVariable = [self stringFromTimeInterval:timeInterval];

    self.dayLable.text = [NSString stringWithFormat:@"%@", stringVariable];
      self.hourLable.text = [NSString stringWithFormat:@"%@", stringVariable];
      self.minutesLable.text = [NSString stringWithFormat:@"%@", stringVariable];
       // i want to update this day hour minutes lable like this much of days hour and minutes remaining 

    NSLog(@"%@",stringVariable);

}

我使用以下方法计算天数小时和分钟,但我不知道什么是逻辑思维不在这里如何找到间隔时间的小时和分钟

  - (NSString *)stringFromTimeInterval:(NSTimeInterval)interval {
        NSInteger ti = (NSInteger)interval;
        NSLog(@"%d",ti);
        //  NSInteger seconds = ti % 60;

         NSInteger days = (ti * 60);

        NSInteger minutes = (ti / 60) % 60;

         NSLog(@"%d",minutes);



        NSInteger hours = (ti / 3600);
         NSLog(@"%d",hours);

        return [NSString stringWithFormat:@"%02i hours : %02i min", hours, minutes];
    }

1 个答案:

答案 0 :(得分:1)

算不算。几天,几个月和几年,适用于以下代码。

double differenceSeconds;
double datediff;
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"YYYY-MM-dd HH:mm:ss ZZZ"];
[df setTimeZone:[NSTimeZone systemTimeZone]];

NSDate *todayDate = [NSDate date];
long long tdate =[todayDate timeIntervalSince1970];
datediff = tdate - yourdate;

int days=(int)((double)datediff/(3600.0*24.00));

int diffDay=datediff-(days*3600*24);

int diffhours=(int)((double)diffDay/3600.00);
int diffMin=diffDay-(diffhours*3600);
int diffminutes=(int)(diffMin/60.0);