.是否有可以引用的农历,或者我在代码中遗漏了一些东西.
override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view,typically from a nib. loopthrough() } func returnDateForMonth(month:NSInteger,year:NSInteger,day:NSInteger)->NSDate{ let comp = NSDateComponents() comp.month = month comp.year = year comp.day = day let chCal = NSCalendar(calendarIdentifier: NSCalendarIdentifierChinese) return chCal!.dateFromComponents(comp)! } func showDate(getDate:NSDate)->String{ let date = getDate //let calendar = NSCalendar.currentCalendar() let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierChinese) let dateComponents = calendar!.components([NSCalendarUnit.Year],fromDate: date) let firstDay = returnDateForMonth(dateComponents.month,year: dateComponents.year,day: 1) let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "dd-MMM-yy" let formattedDate = dateFormatter.stringFromDate(firstDay) //print("First day of this month: \(formattedDate)") // 01-Sep-15 print(formattedDate) return formattedDate } func substractyear(getI:Int)->String{ let getInt = getI * -1 let components: NSDateComponents = NSDateComponents() components.setValue(getInt,forComponent: NSCalendarUnit.Year); let date: NSDate = NSDate() let expirationDate = NSCalendar.currentCalendar().dateByAddingComponents(components,toDate: date,options:NSCalendarOptions(rawValue: 0)) return showDate(expirationDate!) } func loopthrough(){ for var i = 1; i < 100; i++ { //print("\(i)") substractyear(i) } }
产量
31-JAN-14
10-FEB-13
23-JAN-12
03月11
14-FEB-10
26-JAN-09
07月 – 08
18月 – 07
29-JAN-06
09月 – 05
22-JAN-04
01月03
12月 – 02
1月24日 – 01
05 – 2月 – 00
16月99
28-JAN-98
07月 – 97
19-FEB-96
31-JAN-95
10-FEB-94
23-JAN-93
04月 – 92
15月 – 91
1月27日90
06月89
17-FEB-88
29-JAN-87
09月86
20月85
02 – 84
10-FEB-43
22-JAN-42
01-FEB-41
12-FEB-40
1月24日 – 39
04月 – 38
15-FEB-37
28-JAN-36
08 – 2月 – 35
19-FEB-34
31-JAN-33
11-FEB-32
23-JAN-31
03月 – 30
13月29
26-JAN-28
06月27
17月 – 26
29-JAN-25
10-FEB-24
22-JAN-23
01月 – 22
12-FEB-21
25-JAN-20
05 – 2月19
16-FEB-18
1月28日17
08月16
19月15
31-JAN-14
10-FEB-13
23-JAN-12
03月11
14-FEB-10
26-JAN-09
07月 – 08
18月 – 07
29-JAN-06
09月 – 05
22-JAN-04
01月03
12月 – 02
1月24日 – 01
05 – 2月 – 00
16月99
28-JAN-98
07月 – 97
19-FEB-96
31-JAN-95
10-FEB-94
23-JAN-93
04月 – 92
15月 – 91
1月27日90
06月89
17-FEB-88
29-JAN-87
09月86
20月85
02 – 84
10-FEB-43
22-JAN-42
01-FEB-41
12-FEB-40
1月24日 – 39
04月 – 38
15-FEB-37
28-JAN-36
解决方法
let chineseCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierChinese)! let formatter: NSDateFormatter = { let fmt = NSDateFormatter() fmt.dateStyle = .FullStyle fmt.timeStyle = .MediumStyle fmt.dateFormat = "dd-MMM-yy" return fmt }() var comps = chineseCalendar.components([.Year],fromDate: NSDate()) for _ in 0..<100 { comps.year -= 1 guard let newYear = chineseCalendar.dateFromComponents(comps) else { fatalError("no date for \(comps.year)") } print(formatter.stringFromDate(newYear)) }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。