在日期之间做操作数

时间:2017-09-25 09:27:07

标签: ios swift nsdate

我正在做一个秒表,并且需要在事件持续时显示计时器,直到它结束。承认活动从晚上8点开始,当前日期是晚上9点30分,秒表显示01:30:00,并将一直保持滴答,直到结束时间到来。这是我的代码我需要一种方法来给currentTime赋予currentDate- start_time

的值
var currentTime = 0

func printTime() {
    let formatter = DateFormatter()
    formatter.dateFormat = "dd/MM/yyyy"
    let start_time = self.eventArray[self.clubId!]?.start_time
    let end_time = self.eventArray[self.clubId!]?.end_time
    self.currentTime = currentDate - start_time


    if(currentDate?.compare(start_time!) == .orderedAscending && currentDate?.compare(end_time!) == .orderedAscending)
    {
        if(timer != nil)
        {
            timer?.invalidate()
            timer = nil
        }else
        {
            timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { _ in
                self.currentTime += 1
                let hoursPortion = String(format: "%02d", self.currentTime / 60)
                let minutesPortion = String(format: "%02d", self.currentTime / 60)
                let secondsPortion = String(format: "%02d", self.currentTime % 60)
                self.sinceBandeauLabel.text = "\(hoursPortion):\(minutesPortion):\(secondsPortion)"
            })

    }

    }

1 个答案:

答案 0 :(得分:0)

如果start_timeDate,那么您可以使用currentDate.timeIntervalSince(start_time)