gpt4 book ai didi

ios - swift 2 : How to calculate the number of days between current iPhone time and date I provide

转载 作者:行者123 更新时间:2023-11-30 14:01:03 25 4
gpt4 key购买 nike

我看了很多帖子,但都无法解决问题。我需要知道从 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/

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