I have a problem in my application. I am getting a date in this format :
"الإثنين 2015.5.5 - 16:16 مساء بتوقيت ابوظبي"
Like this below Image-
From an API but I want to convert it to the NSDate
English format.
I found this code by @dfri. It converts from English to Arabic but I don't know my API dateFormat
.
// input date in given format (as string)
let inputDateAsString = "2016-03-09 10:33:59"
// initialize formatter and set input date format
var formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
// read input date string as NSDate instance
if let date = formatter.dateFromString(inputDateAsString) {
// set locale to "ar_DZ" and format as per your specifications
formatter.locale = NSLocale(localeIdentifier: "ar_DZ")
formatter.dateFormat = "EEEE, d, MMMM, yyyy HH:mm a"
let outputDate = formatter.stringFromDate(date)
print(outputDate) // الأربعاء, 9 مارس, 2016 10:33 ص
}
How can I do this?
For an easier conversion I think it's better to remove the additional text so I filtered the string and kept only the needed numbers
here is my attempt:
var dateString = "الإثنين 2015.5.5 - 16:16 مساء بتوقيت ابوظبي"
dateString = dateString.trimmingCharacters(in: CharacterSet(charactersIn: "01234567890.-:").inverted)
var formatter = DateFormatter()
formatter.dateFormat = "yyyy.MM.dd - HH:mm"
let date = formatter.date(from: dateString)
print(date)
//"May 5, 2015 at 4:16 PM"