func getDateFrom(_ dateString: String, withFormat formatString: String, timeZone: String = "UTC", locale: String = "UTC") -> Date? {
var dateToReturn: Date?
let formatter = DateFormatter()
formatter.timeZone = NSTimeZone(abbreviation: timeZone) as TimeZone?
formatter.locale = Locale(identifier: locale)
formatter.dateFormat = formatString
dateToReturn = formatter.date(from: dateString)
return dateToReturn
}
func getStringFrom(_ date: Date, withFormat formatString: String, timeZone: String = "UTC", locale: String = "UTC") -> String? {
var stringToReturn: String?
let formatter = DateFormatter()
formatter.timeZone = NSTimeZone(abbreviation: timeZone) as TimeZone?
formatter.locale = Locale(identifier: locale)
formatter.dateFormat = formatString
stringToReturn = formatter.string(from: date)
return stringToReturn
}
let string12H = "2018-12-31 07:35 PM"
if let date = getDateFrom(string12H, withFormat: "yyyy-MM-dd hh:mm a") {
let dateStringWith12HFormat = getStringFrom(date, withFormat: "yyyy-MM-dd hh:mm a", timeZone: "IST", locale: "IST")
let dateStringWith24HFormat = getStringFrom(date, withFormat: "yyyy-MM-dd HH:mm", timeZone: "IST", locale: "IST")
}