0

我正在努力更新 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];
    }
4

1 回答 1

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);
于 2013-07-23T11:51:48.350 回答