0

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

Rocky Balboa
  • 784
  • 10
  • 25
NST
  • 115
  • 10

1 Answers1

0

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!)
Community
  • 1
  • 1
Rocky Balboa
  • 784
  • 10
  • 25