我已經編寫了在過去的100年中獲取中國日曆上的一年中第一天的函數。從循環返回的一些值是正確的。我已通過此鏈接http://www.fengshuimiracle.com/154/how-work-out-your-animal-sign-and-chinese-year 驗證了中國新年的日期。有沒有可以參考的農曆或我在代碼中丟失了什麼。將currentCalendar()日期轉換爲NSCalendarIdentifierChinese
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-FEB-11 14-FEB-10 26-JAN-09 07-二月-08 18月 - 07 29-JAN-06 09 - 2月 - 05 22-JAN-04 01 - 2月03 12月 - 02 1月24日 - 01 05-Feb- 00 16-Feb-99 28-JAN-98 07-FEB-97 19-FEB-96 31-JAN-95 10-FEB-94 23-JAN-93 04-FEB-92 15-FEB-91 27 -Jan-90 06-FEB-89 17-FEB-88 29-JAN-87 09-FEB-86 20-FEB-85 02 - 84 10-FEB-43 22-JAN -42 01-FEB-41 12-FEB-40 24-JAN-39 04-FEB-38 15-FEB-37 28-JAN-36 08-FEB-35 19-FEB-34 31-JAN-33 11-FEB-32 23-JAN-31 03-FEB-30 13-FEB-29 26-JAN-28 06-FEB-27 17 -Feb-26 29-JAN-25 10-FEB-24 22-JAN-23 01-FEB-22 12-FEB-21 25-JAN-20 05-FEB-19 16二月-18 28-JAN-17 08-FEB-16 19-FEB-15 31-JAN-14 10-FEB-13 23-JAN-12 03-FEB-11 14-FEB-10 26-JAN-09 07-FEB-08 18-FEB-07 29-JAN-06 09-FEB-05 22-JAN-04 01-FEB-03 12 -Feb-02 24-JAN-01 05-FEB-00 16-FEB-99 28-JAN-98 07-FEB-97 19-FEB-96 31-JAN-95 2月10日-94 23-JAN-93 04-FEB-92 15-FEB-91 27-JAN-90 06-FEB-89 17-FEB-88 29-揚-87 09-FEB-86 20-FEB-85 02 - 84 10-FEB-43 22-JAN-42 01-FEB-41 12-FEB-40 24 Jan- 39 04 - 2月 - 38 15月 - 37 28-JAN-36
輸出是什麼?哪部分是意外的? – jtbandes
本頁不同意:http://www.chinesenewyears.info/chinese-new-year-calendar.php – jtbandes