Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert currentCalendar() Date to NSCalendarIdentifierChinese

I have written the function to get the 1st day of the year on the Chinese calendar for the past 100 years. Some of the values which are returned from the loop are correct. i have verified the Chinese new year dates with this link http://www.fengshuimiracle.com/154/how-work-out-your-animal-sign-and-chinese-year . is there a lunar calendar which can be referenced or am i missing something in my code.

 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)

    }

}

output

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

like image 676
Mugunthan Balakrishnan Avatar asked Nov 09 '22 23:11

Mugunthan Balakrishnan


1 Answers

I'm not sure if this answers your question, but I think your code can be greatly simplified:

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))
}

And, I'm not sure your original source is correct. This page lists different dates (which seem to better match the NSCalendar output).

like image 112
jtbandes Avatar answered Jan 04 '23 01:01

jtbandes