如何从NSDate获得Day周数

Nav*_*rma 0 objective-c nsdate nsdateformatter ios

我想知道NSDate当天的一周.

示例: - 假设今天是2016年10月10日,这里周数是我使用的第3个

NSInteger week = [_calendar component:NSCalendarUnitWeekOfMonth fromDate:date];
Run Code Online (Sandbox Code Playgroud)

现在我想知道本周的当天数,这是10月10日的第2天.

Raj*_*ari 5

它将获取您的日期编号,即2.每周的天数始终相同.(1-7)

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

//Fetch the day number
NSInteger day = [_calendar component:NSCalendarUnitWeekday fromDate:date];
Run Code Online (Sandbox Code Playgroud)

在此处了解NSCalendar更多详情