How to convert a Persian date to a Gregorian date in a fast

I want to convert data between "Persian date and Gregorian" in fast. I was looking for an algorithm to help me switch between them, but I didn't really understand how to implement them.

If this is an existing way to do work in Swift, please point me to this.

The provision and example will also be appreciated.

+8
source share
3 answers

Update for Swift 3:

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))") 
+15
source

I ran into a problem because I am running swift 2 NOT 3. (now I am updating the code)

here is the fix for the code above so people can see the changes from Swift 2 to Swift 3

 import UIKit let formatter = NSDateFormatter() formatter.dateFormat = "yyy/MM/dd" formatter.calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian) let date = NSDate() let dateInGrogrian = formatter.stringFromDate(date) print("Date in Grogrian = \(dateInGrogrian)") formatter.calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierPersian) formatter.dateFormat = "yyy/MM//dd" print("Converted date to Jalali = \(formatter.stringFromDate(date))") 
+1
source

Swift 4.2 / xCode 10

I have a simple extension for a Persian date as a collector. he has a converter for gregorian. You may be interested.

https://github.com/Fridday/PersianDatePicker

0
source

All Articles