Swift倒数计时器标签,天数/小时/分/秒 [英] Swift countdown timer labels for Days/Hours/Minutes/Seconds

查看:1287
本文介绍了Swift倒数计时器标签,天数/小时/分/秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个倒计时器,计数器在 UIDatePicker 中设置 NSDate 。我有一个标签,显示我们倒计时的日期,并且工作正常。



我还要添加的是整天的标签左边和当天剩下的小时/分钟/秒数(即从不超过23/59/59)这是我在这一刻所做的,但它显然显示了整个倒计时的值。希望有人可以帮助我找出正确的逻辑。

  let secondsLeft = sender.date.timeIntervalSinceDate(NSDate())
hoursLabel.text = String(secondsLeft%3600)
minutesLabel.text = String((secondsLeft / 60)%60)
secondsLabel.text = String(secondsLeft%60)

我想我正在寻找的是一些快速相当于 datetime class你得到php

解决方案

看看NSCalendar类。具体看看方法
components:fromDate:toDate:options:这样可以使用任何你指定的单位来计算两个日期并计算它们之间的差异。 p>

它也是可本地化的,所以如果您使用当前日历,用户使用中文,希伯来语或阿拉伯语日历,那么计算将给您的结果正确的日历。 p>

I'm creating a countdown timer that counts down to an NSDate set in a UIDatePicker. I have a label that shows the date we're counting down to and that works fine.

What I'm also trying to add is labels for the number of whole days left and number of hours/minutes/seconds left in the current day (i.e. never more than 23/59/59) Here's what I've done at the minute but it obviously shows the values for the whole countdown. Hoping someone can help me work out the correct logic here.

let secondsLeft = sender.date.timeIntervalSinceDate(NSDate())
hoursLabel.text = String(secondsLeft % 3600)
minutesLabel.text = String((secondsLeft / 60) % 60)
secondsLabel.text = String(secondsLeft % 60)

I guess what I'm looking for is some swift equivalent of the datetime class you get in php

解决方案

Take a look at the NSCalendar class. Specifically look at the method components:fromDate:toDate:options: That lets you take 2 dates and calculate the difference between them using whatever units you specify.

It's also localizable, so if you use the current calendar and the user uses the Chinese, Hebrew, or Arabic calendar then the calculations will give you results correct for that calendar.

这篇关于Swift倒数计时器标签,天数/小时/分/秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆