我有一个来自API的日期,格式如下:"2016-12-05T05:00:00.000Z"
如何在swift3中找到上述日期和当前日期之间的天数差异?
由于
答案 0 :(得分:0)
我从here
找到答案将其转换为Swift 3
您可以先从上面的字符串中提取日期并存储在start
中。然后将当前日期转换为end
查找当前日期:
let date = NSDate()
var dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
var end = dateFormatter.string(from: date as Date)
找出差异:
var start: String = "2010-09-01"
var f = DateFormatter()
f.dateFormat = "yyyy-MM-dd"
var startDate: Date? = f.date(from: start)
var endDate: Date? = f.date(from: end)
var gregorianCalendar = Calendar(identifier: .gregorian)
var components = gregorianCalendar.dateComponents([.day] ,from: startDate!, to: endDate!)