2015-12-17 12 views
5

Ho scritto la funzione per ottenere il primo giorno dell'anno sul calendario cinese negli ultimi 100 anni. Alcuni dei valori restituiti dal ciclo sono corretti. Ho verificato le date del capodanno cinese con questo link http://www.fengshuimiracle.com/154/how-work-out-your-animal-sign-and-chinese-year . c'è un calendario lunare che può essere referenziato o mi manca qualcosa nel mio codice.Converti currentCalendar() Data in 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) 

    } 

} 

uscita

31-gen-14 10-Feb-13 23-gen-12 03-Feb-11 14-Feb-10 26-gen-09 07- Feb-08 18-Feb-07 29-gen-06 09-Feb-05 22-gen-04 01-Feb-03 12-Feb-02 24-Jan-01 05-FEB 16-feb-99 28-gen-98 07-Feb-97 19-Feb-96 31-gen-95 10-Feb-94 23-gen-93 04-Feb-92 15-Feb-91 -Jan-90 06-Feb-89 17-Feb-88 29-gen-87 09-Feb-86 20-Feb-85 02-Feb-84 10-Feb-43 22-Jan -42 01-Feb-41 12-Feb-40 24-gen-39 04-Feb-38 15-Feb-37 28-gen-36 08-Feb-19-Feb-34 31-gen-33 11-Feb-32 23-gen-31 03-Feb-30 13-Feb-29 26-gen-28 06-Feb-27 17 -Feb-26 29-gen-25 10-Feb-24 22-dic-23 01-Feb-22 12-Feb-21 25-gen-20 05-Feb-19 16-Feb -18 28-gen-17 08-Feb-16 19-Feb-15 31-gen-14 10-Feb-13 23-gen-12 03-Feb-11 14-feb-10 26-gen-09 07-Feb-08 18-feb-07 29-Gen-06 09-Feb-05 22-Gen-04 01-feb-03 12 -Feb-02 24-gen-01 05-Feb-00 16-Feb-99 28-gen-98 07-Feb-97 19-Feb-96 31-gen-95 10-feb -94 23-Jan-93 04-Feb-92 15-Feb-91 27-gen-90 06-Feb-89 17-Feb-88 29- 87-Jan 09-febbraio-86 20-Feb-85 02-Feb-84 10-Feb-43 22-Jan-42 01-Feb-41 12-Feb-40 -Gen-24 39 04-Feb-38 15-Feb-37 28-gen-36

+0

Qual è l'output? Quale parte è inaspettata? – jtbandes

+0

Questa pagina non è d'accordo: http://www.chinesenewyears.info/chinese-new-year-calendar.php – jtbandes

risposta

1

io non sono sicuro se le risposte a questa domanda, ma penso che il codice può essere notevolmente semplificata:

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

E, non sono sicuro che la tua fonte originale sia corretta.This page elenca date diverse (che sembrano corrispondere meglio all'output NSCalendar).

+0

l'output che ottengo dalla data per l'anno 1936 è 28-Jan-36 | che la pagina che hai condiviso mostra 1936-01-24 –

+0

Il codice che ho postato restituisce il 24 gennaio 1936 (e penso che sia molto più facile da capire!). – jtbandes

+0

ha appena eseguito il tuo codice e funziona, che è molto più semplice di quello che ho scritto :) –