Question

I'm trying to obtain the day of the week using the code below with a variable NSString. Whatever value I use the weekday always comes back as 2. What am I doing wrong?

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy"];
NSString *stringDate = @"23/05/2013";
NSDate *dateStr = [dateFormatter dateFromString:stringDate ];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents =[gregorian components:NSWeekdayCalendarUnit fromDate:dateStr];
gWeekday = [weekdayComponents weekday];
Was it helpful?

Solution

The date format does not match your string. With

[dateFormatter setDateFormat:@"dd/MM/yyyy"];
NSString *stringDate =       @"23/05/2013";

you get the correct result.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top