How to get all the days/dates in current week, this month, this year using the current date (today) in iPhone

后端 未结 2 645
日久生厌
日久生厌 2021-01-26 03:48

In my application i have to show list of working hours by date in a table view,

Here i have 5 scenarios

  1. Today\'s projects, working hours
  2. Selected
相关标签:
2条回答
  • 2021-01-26 03:48

    You can use this for getting current year & month. Simply add these to previous answer as my friend had suggested.

    NSInteger year = [comps year];
    NSInteger month = [comps month];
    

    Now, I think if you're getting current year & month, you can set manually for 1st date. and perform as you want.

    Main thing is to get weekday .. Thanks.

    0 讨论(0)
  • 2021-01-26 03:57

    I hope this will give you basic idea how you have to proceed: For other scenarios you can work out

    - (NSDate *)firstDayOfWeekFromDate:(NSDate *)date {
          NSCalendar* calendar = [NSCalendar currentCalendar];
          NSDateComponents* comps = [calendar components:NSYearForWeekOfYearCalendarUnit |NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:date];
    
         [comps setWeekday:2]; // 2: monday
         return [calendar dateFromComponents:comps];
      }
    
    0 讨论(0)
提交回复
热议问题