I have two string , date and time . date string has a date in format "MM-dd-yyyy" and time in format "hh:mm a" , I want to create a 10 digit timestamp from the same . I did the following but I am getting issue with this. Any help is appreciated.
let idate = (userInstance.userData?.Date!)! + "T" + (userInstance.userData?.Time!)! + "+0000"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
let date = dateFormatter.date(from: idate)!
print(date)
let timestamp = Int(date.timeIntervalSince1970)
print(timestamp)
You cannot force a date containing AM/PM time to ISO 8601. ISO 8601 dates are always represented in 24-hour mode.
Besides your order of year, month and day is not ISO 8601 compliant.
Specify the appropriate date format MM-dd-yyyyhh:mm aZ
let datePart = "09-18-2018"
let timePart = "4:22 pm"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "MM-dd-yyyyhh:mm aZ"
let date = dateFormatter.date(from: datePart + timePart + "+0000")!
let timestamp = Int(date.timeIntervalSince1970)
print(timestamp)