将格里高利日期转换为Hijri日期

时间:2015-10-01 17:22:47

标签: swift swift2 nsdateformatter nscalendar

源代码来自:

Hijri (islamic) calendar in swift التاريخ الهجري

How can I convert string date to NSDate?

我只是将它们放在一起,但输出有些错误。

let dateFormatter = NSDateFormatter()

dateFormatter.dateFormat = "YYYY-MM-DD"

let GregorianDate = dateFormatter.dateFromString("\(Yeartext1.text!)-\(Monthtext1.text!)-\(Daytext1.text!)")

let islamic = NSCalendar(identifier: NSIslamicCalendar)

let components = islamic?.components(NSCalendarUnit(rawValue: UInt.max), fromDate: GregorianDate!)

resultlable.text = "\(components!.year) - \(components!.month) - \(components!.day)"

例如,用户将输入格里高利日期(2015/09/29)并且应显示Hijri年份(1436/12/15),但这不起作用。

2 个答案:

答案 0 :(得分:3)

Yeartext1,Monthtext1和Monthtext1是UITextField

结果是UILabel

    let dateFormatter = NSDateFormatter()

    dateFormatter.dateFormat = "dd-MM-yyyy"

    let GregorianDate = dateFormatter.dateFromString("\(Daytext1.text!)-\(Monthtext1.text!)-\(Yeartext1.text!)")

    let islamic = NSCalendar(identifier: NSCalendarIdentifierIslamicUmmAlQura)

    let components = islamic?.components(NSCalendarUnit(rawValue: UInt.max), fromDate: GregorianDate!)

    resultlable.text = "\(components!.year) - \(components!.month) - \(components!.day)"

这将需要一个格里高利日期5-10-2015,它将给你伊斯兰日期1436 - 12 - 22

*一天错误的可能性很小

你可以查看这个转换器 http://www.islamicfinder.org/Hcal/hdate_next.php

答案 1 :(得分:0)

Hijri To Gregorian(这可以帮助某人)

let dateFormatter = NSDateFormatter()

dateFormatter.dateFormat = "dd-MM-yyyy"

dateFormatter.locale = NSLocale(localeIdentifier: "ar_SA")

let islamicDate = dateFormatter.dateFromString("8-10-1437")

let gregorian = NSCalendar(identifier: NSCalendarIdentifierGregorian)

let components = gregorian?.components(NSCalendarUnit(rawValue: UInt.max), fromDate: islamicDate!)

print("\(components!.year) - \(components!.month) - \(components!.day)")