EDIT Start
Ok, obviously I didn't make myself clear.
I need a "first-day-of-the-week"-independent bi-directional mapping of days of the week. So although the user might change his/her locale/region settings the stored day-numbers can still be used to identify the "correct" days.
I know of
NSDateFormatter
's andNSCalendar
'sweekdaySymbols
methods. But I couldn't find any documented direct relation between the indices of the returned array and the value found inNSDateComponents
'weekday
property in the API/Apple docs. Yes, there are a lot of posts (here and elsewhere) doing like[NSCalendar weekdaySymbols][dateComps.weekday - 1]
(or even[NSCalendar weekdaySymbols][dateComps.weekday]
). But, apart from the fact that I had problems mapping the day-IDs back to weekday of an actualNSDateComponents
(see second code snippet from original post), I prefer being sure before handing over the project to a/the client.The problem I encounter with the first code snippet (posted in my original post) is that
NSCalendar
'sdateFromComponents:
doesn't honor theweekday
property of the passedNSDateComponents
instance.
EDIT End
Hi I'm trying to build a day-number (like 1 for sunday) to day-name (like "sunday") mapping. So that I can present the list of days (Monday to Sunday or Sunday to Monday or whatever follows the user's calendar-preferences) to the user. The mapping is needed, so that I can store the number(s) of the days the user selects in a localization independent way.
I know of NSDateFormatter
's or NSCalendar
's weekdaySymbols
. But as I understand that these are just lists, without a relation to the user's preferences (in terms of first-day-of-week) or dayOfWeek (as might be returned by a NSDateComponent
s instance), I don't see how I might use those.
I tried several approaches....but something always breaks the thing up.
The following version for example starts fine with Monday as the first value assigned to dayName
. But further iteration don't seem to have affect on the date. So I get a list of 7 Monday strings.
NSMutableArray *tempArray = [[NSMutableArray alloc] initWithCapacity:7];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
NSDate *date = nil;
NSString *dayName = nil;;
NSDictionary *idToNameDict = nil;
NSInteger weekday = [cal firstWeekday];
for (NSInteger i = 0; i < 7; i++) {
dateComps.weekday = weekday;
date = [cal dateFromComponents:dateComps];
dayName = [dateFormatter stringFromDate:date];
idToNameDict = [NSDictionary dictionaryWithObject:dayName forKey:@( dateComps.weekday )];
[tempArray addObject:idToNameDict];
weekday++;
if (weekday > 7) {
weekday = 1;
}
}
In short, I have the following requirements:
- User-calendar dependent presentation (start/end of week) of days of the week
- Save the days the user selects in a non-localized way (as kind of day-ID)
- Check what ever date if it "matches" one of the days selected by the user before.
I surly have missed something. But I can't seem to find the missing part.
Before I used just the weekdaySymbols
and added 1
to the element-index to map the day-ID to the name. But then the following code hat e.g. weekday
with a value of 2
for a Tuesday, whereas when building up the list Tuesday was associated with 3
.
NSDate *givenDate = ...; // from somewhere
NSInteger userSelectedDay = ...; // from data store
NSDateComponents *comps = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:givenDate];
if (comps.weekday == userSelectedDay) {
// highlight or whatever
}
It might has todo with time-zone "stuff", but as I never set (or remove) the timezone, I can't see how/where.