将公历日期转换为回历日期 [英] Convert gregorian date to Hijri date
问题描述
源代码来自:
我只是将它们放在一起,但是输出有一些错误.
I just put them together but it has some mistake with the output.
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),并应显示回历年(1436/12/15),但这不起作用.
For example, the user will enter a gregorian date (2015/09/29) and it should show the Hijri year (1436/12/15) but this doesn't work.
推荐答案
Yeartext1,Monthtext1和Monthtext1是UITextField
Yeartext1 , Monthtext1 and Monthtext1 are UITextField
可生成的是UILabel
resultlable is 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)"
它需要一个公历2015年5月10日,并且会给您伊斯兰日期1436年12月22日
It will take an Gregorian Date 5-10-2015 and it will give you the islamic date 1436 - 12 - 22
*一天出错的可能性很小
*There is a small probability of one day error
您可以检查此转换器 http://www.islamicfinder.org/Hcal/hdate_next.php
you can check this converter http://www.islamicfinder.org/Hcal/hdate_next.php
这篇关于将公历日期转换为回历日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!