Skip to content

Instantly share code, notes, and snippets.

@lugearma
Created September 29, 2017 18:06
Show Gist options
  • Save lugearma/33052b3cf845d1ff873f37b89dd5fea3 to your computer and use it in GitHub Desktop.
Save lugearma/33052b3cf845d1ff873f37b89dd5fea3 to your computer and use it in GitHub Desktop.
Convert String Date format to Date Type
import Foundation
let start = "2017-05-25"
let end = "2017-11-12"
let dateFormat = "yyyy-MM-dd"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = dateFormat
let startDate = dateFormatter.date(from: start)
let endDate = dateFormatter.date(from: end)
let currentDate = Date()
guard let startDate = startDate, let endDate = endDate else {
fatalError("Date Format does not match ⚠️")
}
if startDate < currentDate && currentDate < endDate {
print("✅")
} else {
print("❌")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment