我有一个应用程序,提供每30秒到期的数据(确切地说,在h/m/s 11:30:00,11:30:30,11:31:00等).
我可以得到当前时间,但我不确定如何计算从现在到最近的30秒之间的时间.
我发现的任何东西都在Objective-C中,我一直无法转换它.
这是我试过的:
func nearestThirtySeconds() -> Date? {
var components = NSCalendar.current.dateComponents([.second], from: self)
let second = components.second ?? 30
components.second = second >= 30 ? 60 - second : -second
return Calendar.current.date(byAdding: components, to: self)
}
Run Code Online (Sandbox Code Playgroud)
但这会返回最近的分钟(我想,它总是会返回一分钟)
有任何想法吗?
swift ×1