Search code examples
swiftnsdate

swift converting date from Gregorian to Islamic not showing correct date


I have this function for converting Gregorian date to Islamic date :

    func toIslamic() -> String {
    let dateFormatter: DateFormatter = DateFormatter()

    dateFormatter.dateFormat = "dd/MM/yyyy"
    dateFormatter.locale = Locale(identifier: "en")
    let GregorianDate = dateFormatter.date(from: "23/03/1992")
    let islamic = Calendar(identifier: Calendar.Identifier.islamicUmmAlQura)
    let components = (islamic as NSCalendar?)?.components(NSCalendar.Unit(rawValue: UInt.max), from: GregorianDate!)
    let fina_date = "\(components!.day!)/\(components!.month!)/\(components!.year!)"
    print("final Islamic Date :: \(fina_date)") // 18/9/1412
    return fina_date

}

the output date is not correct as should be :

func output :

print("final Islamic Date :: \(fina_date)") // 18/9/1412

output should be :

20/9/1412

Is there something that I'm missed ?


Solution

  • There is no guarantee that you will have the correct data during this conversion , your app should provide a correction part that the user can select +1 , +2 ,-1 and -2 to the current displayed date that you should add to the date you get from conversions , and this is found even in most famous apps like MuslimPro