I have a date coming from an API in the following format: "2016-12-05T05:00:00.000Z"
How can I find the difference in days between the above date and current date in swift3?
Thanks
I have a date coming from an API in the following format: "2016-12-05T05:00:00.000Z"
How can I find the difference in days between the above date and current date in swift3?
Thanks
I found the answer from here
Converted it into Swift 3
You can first extract the date from above string and store in start
. Then take the current date into end
Find Current Date:
let date = NSDate()
var dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
var end = dateFormatter.string(from: date as Date)
Find Difference:
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!)