Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a Persian date into a Gregorian in swift

Tags:

date

ios

swift

I want to convert data between "Persian date and Gregorian" in swift. I was looking into algorithm to help me convert between them, but I did not really understand how to implement them.

If their is an existing way to do the job in Swift please point me to it.

Providing and example will be also appreciated.

like image 497
Mohsen Avatar asked Dec 07 '16 20:12

Mohsen


1 Answers

For Swift 5: Format is little change for year

let formatter = DateFormatter()
formatter.dateFormat = "dd/MM/YYYY"
formatter.calendar = Calendar(identifier: .gregorian)
let date = Date()
let dateInGrogrian = formatter.string(from: date)

print(dateInGrogrian)

formatter.calendar = Calendar(identifier: .persian)
formatter.dateFormat = "dd/MM/YYYY"
print("Converted date to Hijri = \(formatter.string(from: date))")
like image 83
Abdul Karim Khan Avatar answered Sep 21 '22 16:09

Abdul Karim Khan