How to convert a Persian date into a Gregorian in swift

后端 未结 3 1816
面向向阳花
面向向阳花 2021-01-02 08:08

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

相关标签:
3条回答
  • 2021-01-02 08:45

    I run into issue because I am running swift 2 NOT 3.(I am updatingXcode now)

    here is the fix for the code above for people to 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))")
    
    0 讨论(0)
  • 2021-01-02 09:02

    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))")
    
    0 讨论(0)
  • 2021-01-02 09:04

    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))")
    
    0 讨论(0)
提交回复
热议问题