Swift倒计时器 - 显示剩余的天数小时

时间:2021-01-24 02:47:49

Hello I am very new to swift and I was trying to create an app that counts down to an event on a specific date. I want it to show the number of days hours and seconds left until the specified date but I cannot figure out how to do this.

您好我是swift的新手,我正在尝试创建一个可以计算特定日期事件的应用程序。我希望它显示在指定日期之前剩余的小时数和秒数,但我无法弄清楚如何执行此操作。

Please help!

请帮忙!

3 个解决方案

#1


6  

This works like a charm:

var releaseDate: NSDate?

override func viewDidLoad() {
    super.viewDidLoad()

    let releaseDateString = "2016-03-02 22:00:00"
    let releaseDateFormatter = NSDateFormatter()
    releaseDateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    releaseDate = releaseDateFormatter.dateFromString(releaseDateString)!

    NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "countDownDate", userInfo: nil, repeats: true)
}

func countDownDate() {
    let currentDate = NSDate()

    let diffDateComponents = NSCalendar.currentCalendar().components([NSCalendarUnit.Month, NSCalendarUnit.Day, NSCalendarUnit.Hour, NSCalendarUnit.Minute, NSCalendarUnit.Second], fromDate: currentDate, toDate: releaseDate!, options: .MatchFirst)

    let countdown = "Months: \(diffDateComponents.month), Days: \(diffDateComponents.day), Hours: \(diffDateComponents.hour), Minutes: \(diffDateComponents.minute), Seconds: \(diffDateComponents.second)"
    print(countdown)
}

#2


4  

Swift 3

斯威夫特3

var releaseDate: Date? 

override func viewDidLoad() {
  super.viewDidLoad()     

  let releaseDateFormatter = DateFormatter()
  releaseDateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  releaseDate = releaseDateFormatter.date(from:releaseDateString!)!

  Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.countDownDate), userInfo: nil, repeats: true)
}

func countDownDate() {
    let date = Date()
    let calendar = Calendar.current

    let diffDateComponents = calendar.dateComponents([.day, .hour, .minute, .second], from: date, to: releaseDate!)
    let countdown = "Days \(diffDateComponents.day),  Hours: \(diffDateComponents.hour), Minutes: \(diffDateComponents.minute), Seconds: \(diffDateComponents.second)"
    print(countdown)
}

#3


0  

Swift 4

斯威夫特4

var releaseDate: NSDate?
var countdownTimer = Timer()

func startTimer() {

    let releaseDateString = "2018-09-16 08:00:00"
    let releaseDateFormatter = DateFormatter()
    releaseDateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    releaseDate = releaseDateFormatter.date(from: releaseDateString)! as NSDate

    countdownTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
}

@objc func updateTime() {

    let currentDate = Date()
    let calendar = Calendar.current

    let diffDateComponents = calendar.dateComponents([.day, .hour, .minute, .second], from: currentDate, to: releaseDate! as Date)

    let countdown = "Days \(diffDateComponents.day ?? 0), Hours \(diffDateComponents.hour ?? 0), Minutes \(diffDateComponents.minute ?? 0), Seconds \(diffDateComponents.second ?? 0)"

    print(countdown)
}

#1


6  

This works like a charm:

var releaseDate: NSDate?

override func viewDidLoad() {
    super.viewDidLoad()

    let releaseDateString = "2016-03-02 22:00:00"
    let releaseDateFormatter = NSDateFormatter()
    releaseDateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    releaseDate = releaseDateFormatter.dateFromString(releaseDateString)!

    NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "countDownDate", userInfo: nil, repeats: true)
}

func countDownDate() {
    let currentDate = NSDate()

    let diffDateComponents = NSCalendar.currentCalendar().components([NSCalendarUnit.Month, NSCalendarUnit.Day, NSCalendarUnit.Hour, NSCalendarUnit.Minute, NSCalendarUnit.Second], fromDate: currentDate, toDate: releaseDate!, options: .MatchFirst)

    let countdown = "Months: \(diffDateComponents.month), Days: \(diffDateComponents.day), Hours: \(diffDateComponents.hour), Minutes: \(diffDateComponents.minute), Seconds: \(diffDateComponents.second)"
    print(countdown)
}

#2


4  

Swift 3

斯威夫特3

var releaseDate: Date? 

override func viewDidLoad() {
  super.viewDidLoad()     

  let releaseDateFormatter = DateFormatter()
  releaseDateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  releaseDate = releaseDateFormatter.date(from:releaseDateString!)!

  Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.countDownDate), userInfo: nil, repeats: true)
}

func countDownDate() {
    let date = Date()
    let calendar = Calendar.current

    let diffDateComponents = calendar.dateComponents([.day, .hour, .minute, .second], from: date, to: releaseDate!)
    let countdown = "Days \(diffDateComponents.day),  Hours: \(diffDateComponents.hour), Minutes: \(diffDateComponents.minute), Seconds: \(diffDateComponents.second)"
    print(countdown)
}

#3


0  

Swift 4

斯威夫特4

var releaseDate: NSDate?
var countdownTimer = Timer()

func startTimer() {

    let releaseDateString = "2018-09-16 08:00:00"
    let releaseDateFormatter = DateFormatter()
    releaseDateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    releaseDate = releaseDateFormatter.date(from: releaseDateString)! as NSDate

    countdownTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
}

@objc func updateTime() {

    let currentDate = Date()
    let calendar = Calendar.current

    let diffDateComponents = calendar.dateComponents([.day, .hour, .minute, .second], from: currentDate, to: releaseDate! as Date)

    let countdown = "Days \(diffDateComponents.day ?? 0), Hours \(diffDateComponents.hour ?? 0), Minutes \(diffDateComponents.minute ?? 0), Seconds \(diffDateComponents.second ?? 0)"

    print(countdown)
}