Reputation: 6451
I'm trying to create a function that will get the Gregorian start and end dates for a month passed in of any arbitrary calendar.
So if I pass Hebrew calendar, month 3, year 5775, I would expect back something like this (2015-05-19T00:00:00.000-05:00, 2015-06-17T00:00:00.000-05:00)
let (start,end) = gregorianMonthForCalendarMonth(NSCalendar(calendarIdentifier: NSCalendarIdentifierHebrew)!, 3, 5775)
println(start)
println(end)
What I'm getting is (2014-11-23T00:00:00.000-05:00, 2014-12-22T00:00:00.000-05:00)
Not sure what I'm doing wrong.
My functions:
let fullDateFormatter = getFullDateFormatter()
func getFullDateFormatter() -> NSDateFormatter {
let _dateFormatter = NSDateFormatter()
_dateFormatter.calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
_dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'.'SSSZZZZZ"
return _dateFormatter
}
func gregorianMonthForCalendarMonth(calendar:NSCalendar, month:Int, year:Int) -> (start:String, end:String) {
// get 1st day of month
let components = NSDateComponents()
components.year = year
components.month = month
components.day = 1
let start = calendar.dateFromComponents(components)
// get number of days in the month
let days = calendar.rangeOfUnit(NSCalendarUnit.CalendarUnitDay, inUnit: NSCalendarUnit.CalendarUnitMonth, forDate: start!)
components.day = days.length
let end = calendar.dateFromComponents(components)
return (start!.stringValue(),end!.stringValue())
}
extension NSDate {
func stringValue() -> String {
let strDate = fullDateFormatter.stringFromDate(self)
return strDate
}
}
Upvotes: 1
Views: 95
Reputation: 437582
The problem would appear to stem from the interpretation of what the "third" month of 5775 is. Kislev is the 3rd month of the civil year. And 1 Kislev 5775 is 23 November 2014.
However, Silvan is the 3rd month of the ecclesiastical year and 1 Silvan 5775 is 19 May 2015.
Apple's choice of the civil year month numbering is somewhat understandable as the Hebrew numeric year value increments on Tishrei (the first month of the civil year, the seventh month of the ecclesiastical year).
Admittedly, many Hebrew date conversion sites list the months in the order they occur in the ecclesiastical year, but if you dive into those sites, you'll see that they accurately reflect the year number changing on the first of Tishrei. I've noticed that many of these sites don't ask for numeric month number, but rather let you select from a list of month names, eliminating the sort of confusion that simply referring to the "third" month might otherwise entail.
Upvotes: 1