当前位置:  开发笔记 > 编程语言 > 正文

将currentCalendar()Date转换为NSCalendarIdentifierChinese

如何解决《将currentCalendar()Date转换为NSCalendarIdentifierChinese》经验,为你挑选了0个好方法。

我已经写了这个函数,以便在过去的100年中在中国历法上获得一年的第一天.从循环返回的一些值是正确的.我已通过此链接验证了中国新年的日期http://www.fengshuimiracle.com/154/how-work-out-your-animal-sign-and-chinese-year .是否有可以引用的农历,或者我在代码中遗漏了一些东西.

 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-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 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-Feb-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-Feb-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 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-Feb-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

推荐阅读
拾味湖
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有