Datum - Ostersonntag berechnen
let Karfreitag = calcEasterDate(date:Date() , DELTA: -2)
let Ostersonntag = calcEasterDate(date:Date() , DELTA: 0)
let Ostermontag = calcEasterDate(date:Date() , DELTA: 1)
let Himmelfahrt = calcEasterDate(date:Date() , DELTA: 39)
let Pfingstmontag = calcEasterDate(date:Date() , DELTA: 50)
//Berechnung Ostersonntag für den Gregorianischen Kalender
func calcEasterDate(date: NSDate, DELTA: Int) -> (easterSunday: NSDate?, YEAR: Int, MONTH: Int, DAY: Int) {
let calendar = Calendar(identifier: .gregorian)
var dateComponents = calendar.dateComponents([.day, .month, .year], from: date as Date)
//ermittelt Ostersonntag für den Gregorianischen Kalender
let YEAR: Int = dateComponents.year!
var M: Int
var N: Int
var MONTH: Int = 0
var DAY: Int = 0
switch YEAR {
case 1700 ... 1799:
M=23
N=3
break
case 1800 ... 1899:
M=23
N=4
break
case 1900 ... 1999:
M=24
N=5
break
case 2000 ... 2099:
M=24
N=5
break
case 2100 ... 2199:
M=24
N=6
break
case 2200 ... 2299:
M=25
N=0
break
case 2300 ... 2399:
M=26
N=1
break
case 2400 ... 2499:
M=25
N=1
break
default:
M=0
N=0
return (nil, 0,0,0)
}
//log.msg("year:\(YEAR) M:\(M) N:\(N)")
let a: Int = YEAR % 19;
let b: Int = YEAR % 4;
let c: Int = YEAR % 7;
let d: Int = (19 * a + M) % 30;
let e: Int = ( 2 * b + 4 * c + 6 * d + N) % 7;
let EiM: Int = (22 + d + e);
var EiA: Int = (d + e - 9);
if (EiM >= 1) && (EiM <= 31) {
//Ostern im März
MONTH = 3
DAY = EiM
} else {
//Ostern im April
MONTH = 4
if (EiA == 26){
//Sonderregelung 1 (1981, 2076, 2133)
EiA = 19
}
if (d==28 && e==6 && (11*M+11) % 30 < 19 ){
//Sonderregelung 2 (1954, 2049 und 2106)
EiA = 18
}
DAY = EiA
}
let dateString = "\(String(YEAR))-\(String(format: "%02i", MONTH))-\(String(format: "%02i", DAY))"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
var easterday: Date = dateFormatter.date(from: dateString)!
easterday.addTimeInterval(TimeInterval(DELTA * (24 * 60 * 60) )) //Delta in Tagen zum Ostersonntag addieren
dateComponents = calendar.dateComponents([.day, .month, .year], from: easterday)
//log.msg("easterday: \(dateComponents)")
return (easterday as NSDate?, dateComponents.year!, dateComponents.month!, dateComponents.day!)
}
Datum - einen Tag addieren
// *** Create date ***
let date = Date()
// *** create calendar object ***
var calendar = NSCalendar.current
let tomorrow = calendar.current.date(byAdding: .day, value: 1, to: date)
...
Datum - einzelne Komponenten
// *** Create date ***
let date = NSDate()
// *** create calendar object ***
var calendar = NSCalendar.current
// *** Get components using current Local & Timezone ***
var dateComponents = calendar.dateComponents([.year, .month, .day, .hour, .minute], from: date as Date)
var MINUTE: Int = dateComponents.minute!
var HOUR: Int = dateComponents.hour!