Search code examples
iosobjective-cnsdatenscalendar

How to get start date of current year in iOS


I don't know how to use NSDate or NSCalendar in this condition.
Help me to solve this issue.


Solution

  • -(NSDate *)getFirstDateOfCurrentYear
     {
      //Get current year
      NSDate *currentYear=[[NSDate alloc]init];
      currentYear=[NSDate date];
      NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
      [formatter1 setDateFormat:@"yyyy"];
      NSString *currentYearString = [formatter1 stringFromDate:currentYear];
    
      //Get first date of current year
      NSString *firstDateString=[NSString stringWithFormat:@"10 01-01-%@",currentYearString];
      NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
      [formatter2 setDateFormat:@"hh dd-MM-yyyy"];
      NSDate *firstDate = [[NSDate alloc]init];
      firstDate = [formatter2 dateFromString:firstDateString];
    
      return firstDate;
     }