views:

2406

answers:

8

Does anyone know if there is a way to set the first day of the week on a NSCalendar, or is there a calendar that already has Monday as the first day of the week, instead of Sunday. I'm currently working on an app that is based around a week's worth of work, and it needs to start on Monday, not Sunday. I can most likely do some work to work around this, but there will be a lot of corner cases. I'd prefer the platform do it for me.

Thanks in advance

Here's some the code that I'm using. it's saturday now, so what I would hope is that weekday would be 6, instead of 7. that would mean that Sunday would be 7 instead of rolling over to 0

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setFirstWeekday:0];
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit;
NSDateComponents *todaysDate = [gregorian components:unitFlags fromDate:[NSDate date]];
int dayOfWeek = todaysDate.weekday;
+2  A: 

setFirstWeekday: on the NSCalendar object. Sets the index of the first weekday for the receiver.

- (void)setFirstWeekday:(NSUInteger)weekday

Should do the trick.

mmc
I tried setting this value to various values, and it has no effect on the weekday property of the NSCalendary. I've added some code to my original post so you can see what I'm doing.thanks
Joshua
sorry, I meant the weekday property on NSDateComponents
Joshua
A: 

Try this:

NSCalendar *yourCal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]
[yourCal setFirstWeekday:0];
nicktmro
A: 

Without having tested the following code, I suggest this:

[gregorian setFirstWeekDay:1];

The system probably has Sunday placed as the first day (at index 0), which means Monday would be at index 1.

MW
Without having verified the above code, if I had to code week days as integers I would set Monday as Sunday+1, i.e., 1 if Sunday is 0.
mouviciel
*facepalm*Of course. Edited.
MW
A: 

… is there a calendar that already has Monday as the first day of the week, instead of Sunday.

Someday, there will be.

Peter Hosey
A: 

I see misunderstanding in the other messages. The first weekday, whichever it is, has number 1 not 0. By default Sunday=1 as in the "Introduction to Date and Time Programming Guide for Cocoa: Calendrical Calculations":

"The weekday value for Sunday in the Gregorian calendar is 1"

For the Monday as a first workday the only remedy I have is brute force condition to fix the calculation

NSCalendar *cal=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [cal components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
// set to 7 if it's Sunday otherwise decrease weekday number
NSInteger weekday=[comps weekday]==1?7:[comps weekday]-1;
ciukes
+1  A: 

This code constructs a date that is set to Monday of the current week:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDate *today = [NSDate date];
NSDate *beginningOfWeek = nil;
BOOL ok = [gregorian rangeOfUnit:NSWeekCalendarUnit startDate:&beginningOfWeek
              interval:NULL forDate: today];
Kendall Helmstetter Gelner
+1  A: 

Hi, Iv found out the way to display any weekday name using nscalender..using the following code.. Just open your console from xcode menu bar to see the results.Copy Paste the following code in your viewDidLoad method to get the first day of the week

NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy :EEEE"];
NSString *dateString = [dateFormat stringFromDate:today];
NSLog(@"date: %@", dateString);
[dateFormat release];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *components = [gregorian components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today];
[components setDay:([components day]-([components weekday]-1))];

NSDate *beginningOfWeek = [gregorian dateFromComponents:components];
NSDateFormatter *dateFormat_first = [[NSDateFormatter alloc] init];
[dateFormat_first setDateFormat:@"MM/dd/yyyy :EEEE"];
NSString *dateString_first = [dateFormat_first stringFromDate:beginningOfWeek];
NSLog(@"First_date: %@", dateString_first);

The Output will be:

 date: 02/11/2010 :Thursday
 First_date: 02/07/2010 :Sunday

since i had run this program on 2/11/2010 u will get the desired output depending on the current date.

Similarly if u want to get the first working day of the week i.e Monday's date then just modify the code a bit:

CHANGE :[components setDay:([components day]-([components weekday]-1))];

TO [components setDay:([components day]-([components weekday]-2))];

to get Mondays date for that week..

Similarly u can try to find the date of any of seven workdays by changing the integer -1,-2 and so on...

Hope u r question is answered..

Thanks, Bonson Dias

Bonson
A: 

In my opinion this settings should be dynamic according to the user locale. Therefore one should use:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setLocale:[NSLocale currentLocale]];

This will cause the calendar to set the first week day according to the user locale automatically. Unless you are developing your app for a specific purpose/user locale (or prefer to allow the user to choose this day).

Amir Naor