Navneet Sharma Navneet Sharma - 1 month ago 7
Objective-C Question

How to get Day number of week from NSDate

I want to know the day number of week from NSDate.

Example:- Suppose today is 10th Oct 2016, Here Week number is 3rd which i got using

NSInteger week = [_calendar component:NSCalendarUnitWeekOfMonth fromDate:date];


Now I want to know the day number of current week, Which is 2nd for 10th Oct.

Answer

It will fetch you the day number i.e. 2. The day number are always same for every week. (1-7)

//Assuming your calendar is initiated like this
NSCalendar *_calendar = [NSCalendar currentCalendar];

//Fetch the day number
NSInteger day = [_calendar component:NSCalendarUnitWeekday fromDate:date];

Explore NSCalendar for more details here

Comments