【发布时间】:2015-10-07 04:14:13
【问题描述】:
我有以下代码:
//Age
@IBOutlet weak var daysLabel: UILabel!
@IBOutlet weak var monthsLabel: UILabel!
@IBOutlet weak var yearsLabel: UILabel!
@IBAction func birthday(sender:AnyObject){
var dateComponents = NSDateComponents()
dateComponents.day = 19
dateComponents.month = 12
dateComponents.year = 2015
let certainDate = NSCalendar(identifier: NSCalendarIdentifierGregorian)!.dateFromComponents(dateComponents)!
let durationDateComponents = NSCalendar(identifier: NSCalendarIdentifierGregorian)!.components( [.Year, .Month, .Day, .Hour, .Minute, .Second], fromDate: NSDate(), toDate:certainDay, options: [])
yearsLabel.text = "\(durationDateComponents.year)"
monthsLabel.text = "\(durationDateComponents.month)"
daysLabel.text = "\(durationDateComponents.day)"
}
这段代码在计算从那天起已经过了多少天,但它不会倒计时,这是为什么呢?我如何做到这一点?
【问题讨论】:
-
如果我将
certainDay更改为birthDate我得到了距离该日期的年数、月数和天数 - 你想要什么? -
不,应该是确定的日期。而且它不会显示日期的年、月和日,它会出于某种原因从日期显示它。
-
我运行了你的代码,得到了 0 年 2 个月 11 天,这对我来说似乎是正确的
-
@Paulw11,我有 18 天 1 小时 2 分钟,它就卡在那里了。完全相同的代码。
-
这是我操场上的直接复制粘贴 - gist.github.com/paulw11/d6b1be47d3f6bc20eaf6。您的代码没有说明小时、分钟或秒