Datepicker picks wrong date
This is fine because the date picker uses its local time but it stores UTC time. It means your local time offset is -5h. You can do as follow to extract the right date and time from it:
extension NSDate {
var localizedDescription: String {
return descriptionWithLocale(NSLocale.currentLocale()) ?? ""
}
}
println(chosenDate.localizedDescription)
Swift 3 or later
extension Date {
var localizedDescription: String {
return description(with: .current)
}
}
Date().localizedDescription // "Monday, October 16, 2017 at 2:24:11 AM Brasilia Summer Time"
Swift 3.0 - If You want to get a result in date format then use the following code.
extension Date {
var convertedDate:Date {
let dateFormatter = DateFormatter();
let dateFormat = "dd MMM yyyy";
dateFormatter.dateFormat = dateFormat;
let formattedDate = dateFormatter.string(from: self);
dateFormatter.locale = NSLocale.current;
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+0:00");
dateFormatter.dateFormat = dateFormat as String;
let sourceDate = dateFormatter.date(from: formattedDate as String);
return sourceDate!;
}
}
print(pickerView.date.convertedDate)