代码之家  ›  专栏  ›  技术社区  ›  fisher

时间戳。两次之间的差异

  •  1
  • fisher  · 技术社区  · 11 年前

    我正在尝试制作一个时间戳应用程序,你可以通过一个按钮捕捉时间。

    当按下按钮时,它会打印出当前时间 00:00:00 . 当第二次按下按钮时,它会再次打印出当前时间(即 00:00:15 ).

    然后我想让它计算两个时间戳之间的时间差(10秒)。

    我是objective-c的新手,我已经处理这个小问题好几个小时了。如果有一个指向正确方向的指针就好了。

    我在 timeIntervalSinceDate 。这是代码:

    - (IBAction)checkButton:(id)sender {
    
    
    if (buttonPress) {
    
        self.checkInStamp = [NSDate date];
    
        NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
        [timeFormatter setTimeStyle:NSDateFormatterMediumStyle];
    
        checkInTime.text = [timeFormatter stringFromDate:checkInStamp];
        buttonPress = false;
    }
        else {
    
            self.checkOutStamp = [NSDate date];
    
            NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
            [timeFormatter setTimeStyle:NSDateFormatterMediumStyle];
    
            checkOutTime.text = [timeFormatter stringFromDate:checkOutStamp];
    
            NSTimeInterval timeDifference = [checkOutStamp timeIntervalSinceDate:checkInStamp];
    
            totalDuration.text = @"%d", timeDifference; //<-This gives "Expression result unused on build"
        }
    }
    

    谢谢你的帮助!

    1 回复  |  直到 11 年前
        1
  •  0
  •   trojanfoe    11 年前

    两者之间的区别 NSDate 对象是使用 [NSDate timeIntervalSinceDate:] 方法( reference )并表示为 NSTimeInterval (a) typedef '天 double ):

    NSTimeInterval difference = [self.checkOutStamp timeIntervalSinceDate:self.checkInStamp];
    NSLog(@"Difference is %f seconds", difference);
    
    推荐文章