Suppose we want to find the day of the week according to the given date i.e if we want to know the week day of a particular date then the following lines of code will help you to implement this :-
NSDate *date = [NSDate date]; // get todays date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init] ;
[dateFormatter setDateFormat:@"EEEE"]; //set the dateFormat
NSLog(@"Week day of selected date -> %@", [dateFormatter stringFromDate:date]);
We can also get the number of day i.e if the day is TUESDAY then the output will be 3. The following code will implement it :-
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *comps = [gregorian components:NSCalendarUnitWeekday fromDate:[NSDate date]];
NSInteger weekday = [comps weekday];
NSLog(@"Number of day = %ld",(long)weekday);
The output for the above lines of code will be :-
Week day of selected date -> Tuesday
Number of day = 3
0 Comment(s)