1

In my app, want to get the day (i.e. Sunday, Monday,etc.) from the date.

My code is as follow:

NSDate *currentDate = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];

    //ask for current week
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    comps=[calendar components:NSWeekCalendarUnit|NSYearCalendarUnit fromDate:currentDate];
    //create date on week start
    NSDate* weekstart=[calendar dateFromComponents:comps];

    //add 7 days
    NSMutableArray* week=[NSMutableArray arrayWithCapacity:7];
    for (int i=1; i<=7; i++) {
        NSDateComponents *compsToAdd = [[NSDateComponents alloc] init];
        compsToAdd.day=i;
        NSDate *nextDate = [calendar dateByAddingComponents:compsToAdd toDate:weekstart options:0];
        NSLog(@"%@",nextDate);  // date is 2013-06-30 18:30:00 +0000



        NSDateFormatter *myFormatter = [[NSDateFormatter alloc] init];
        [myFormatter setDateFormat:@"EEEE"]; // day, like "Saturday"
        //[myFormatter setDateFormat:@"c"]; // day number, like 7 for saturday

        NSString *dayOfWeek = [myFormatter stringFromDate:nextDate];

PROBLEM

        NSLog(@"Today is: %@", dayOfWeek);  // Prints Mondays instead of Sunday
    }

How can I solve this?

Jonathan
  • 20,053
  • 6
  • 63
  • 70
user2526811
  • 1,233
  • 4
  • 20
  • 54
  • Is a consistent locale / time zone applied throughout your source data and calculations? – Wain Jul 01 '13 at 11:53
  • Have you figured out what was wrong? How did you resolve this? I am having the same issue.. – Geekoder Apr 30 '14 at 09:51
  • Possible duplicate of [How do I get the day of the week with Cocoa Touch?](http://stackoverflow.com/questions/4269093/how-do-i-get-the-day-of-the-week-with-cocoa-touch) – Suhaib Oct 05 '16 at 06:23

7 Answers7

6

Try this :

    NSCalendar* calender = [NSCalendar currentCalendar];
    NSDateComponents* component = [calender components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
    return [component weekday]; // 1 for Sunday, 2 for Monday, and so on...

and see also this Day Name From NSDate? and this https://discussions.apple.com/thread/1700102?start=0&tstart=0

Edit

try this

NSString * time = @"2013-06-30T11:00:26+0100";

NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date1 = [df dateFromString:time];
long lgTime = (long)[date1 timeIntervalSince1970];
NSLog(@"%ld", lgTime);


NSCalendar* calender = [NSCalendar currentCalendar];
NSDateComponents* component = [calender components:NSWeekdayCalendarUnit fromDate:date1];

NSLog(@"date=%@ === %@",[NSDate date],date1);
NSLog(@"day= %d",[component weekday]); ///it return 1
Community
  • 1
  • 1
SAMIR RATHOD
  • 3,512
  • 1
  • 20
  • 45
6
 NSCalendar* calender = [NSCalendar currentCalendar];

 NSDateComponents* component = [calender components:NSWeekdayCalendarUnit fromDate:[NSDate date]];

int weekDay = [component weekday];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSString *weekdayString = [[formatter weekdaySymbols] objectAtIndex:weekDay - 1];

NSLog(@"Day ::: %@", weekdayString);
Haseeb Afsar
  • 619
  • 5
  • 7
5
NSDateFormatter* theDateFormatter = [[NSDateFormatter alloc] init];
[theDateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[theDateFormatter setDateFormat:@"EEEE"];
NSString *weekDay =  [theDateFormatter stringFromDate:[NSDate date]];
karthika
  • 4,085
  • 3
  • 21
  • 23
1

I have made a small NSDate category that helps you find the weekday name from a date. It returns the localised string for the names of the weekdays. Have a look at it. You can use it if you want to.

NKLocalizedWeekday+NSDate

Basically, this is the code you need:

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
int weekdayNumber = [components weekday];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSString *weekdayString = [[formatter weekdaySymbols] objectAtIndex:weekdayNumber - 1];
Nikola Kirev
  • 3,152
  • 3
  • 22
  • 30
0
mydate = [NSDate date];
NSDateFormatter *dt2 = [ [NSDateFormatter alloc] init];
[dt2 setDateFormat:@"EEEE"];

NSString *day = [[NSString alloc] initWithFormat:@"%@",[dt2 stringFromDate:mydate]];
Trup
  • 635
  • 5
  • 17
0

you face problem because it take local time zone

try this

  NSDateFormatter *dateformate = [[NSDateFormatter alloc] init] ;
 [dateformate setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Sydney"]];
 [dateformate setDateFormat:@"EEEE"];
  NSLog(@"%@", [dateformate stringFromDate:[NSDate date]]);
  NSLog(@"%@", [NSTimeZone knownTimeZoneNames]);

set time zone that you required.it may work and help you.

similar problem in NSCalendar first day of week link refer it.also may help you.

Community
  • 1
  • 1
user1548843
  • 670
  • 12
  • 20
0

i am using this and it worked for me in iOS 8.4

-(NSString *) extractDayFromDate {
     NSCalendar* calender = [NSCalendar currentCalendar];
     NSDateComponents* component = [calender components:NSCalendarUnitWeekday fromDate:[NSDate date]];
     return [[calender weekdaySymbols] objectAtIndex:([component weekday]-1)]; 
}
devjangir
  • 11
  • 2