Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert english date to arabic date ios swift

enter image description herein my app i am getting date in this format = "2016-02-15 10:49:59" bu i want to display it in this format "الأربعاء, 9 مارس, 2016 10:33 ص" so how can i do this?

i mage showing the format in which i want iot

like image 691
Govind Rakholiya Avatar asked Mar 10 '16 08:03

Govind Rakholiya


1 Answers

You can make use of NSDateFormatter and locale "ar_DZ", with a custom format specification to fit your needs: "EEEE, d, MMMM, yyyy HH:mm a".

// 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 ص
}

Note that the above uses the default gregorian calendar (in so not translating e.g. year 2016 to year 1437 (/1438 ~October 2016) in the islamic calendar).


(Edit addition regarding your comment below)

If you change localeIdentifier above from "ar_DZ" to "ar", also numeric values gets written in arabic characters:

print(outputDate) // الأربعاء, ٩ مارس, ٢٠١٦ ١٠:٣٣ ص

However, I don't know arabic, so I can't really say if your image above displays that, and I'm no longer certain what you're trying to achieve; possibly this is not it.

like image 129
dfrib Avatar answered Oct 09 '22 20:10

dfrib