gpt4 book ai didi

快速倒数计时器 - 显示剩余的天数小时秒数

转载 作者:搜寻专家 更新时间:2023-10-31 08:11:54 26 4
gpt4 key购买 nike

你好,我是 swift 的新手,我正在尝试创建一个应用程序来为特定日期的事件倒计时。我希望它显示指定日期之前剩余的天数、小时数和秒数,但我不知道该怎么做。

求助!

最佳答案

swift 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)
}

关于快速倒数计时器 - 显示剩余的天数小时秒数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35284913/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com