作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我看了很多帖子,但都无法解决问题。我需要知道从 iPhone 的日期和我提供的较旧日期算起,过去了多少天/小时/分钟。我尝试了不同的教程并在 stackoverflow 上发布,但没有任何效果。请帮帮我谢谢。
class timeElapsed {
init?(startDate: NSDate, endDate: NSDate){
let calendar = NSCalendar.currentCalendar()
let components = calendar.components([.Day, .Month, .Year], fromDate: startDate, toDate: endDate, options: [])
print(components.day)
return nil
}
使用此代码,它可以工作,但它给了我 7 天的差异,而不是 11 天的差异。事实上,日期是 endDate: 2015-10-07 14:27:53 +0000
startDate: 2015-09-26 15:17:55 +0000
最佳答案
这应该有效:
func timeBetween(startDate: NSDate, endDate: NSDate) -> [Int]
{
let calendar = NSCalendar.currentCalendar()
let components = calendar.components([.Day, .Month, .Year], fromDate: startDate, toDate: endDate, options: [])
return [components.day, components.hour, components.minute]
}
您传递开始日期和结束日期并返回一个整数数组。
let nowDate:NSDate // current date
let historydate:NSDate // some old date
let timeSince:[Int] = timeBetween(historydate, endDate: nowDate)
timeSince[0] // days
timeSince[1] // hours
timeSince[2] // minutes
关于ios - swift 2 : How to calculate the number of days between current iPhone time and date I provide,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32994063/
我是一名优秀的程序员,十分优秀!