I don't know how to use NSDate or NSCalendar in this condition.
Help me to solve this issue.
This can be done much cleaner than the other answers:
NSDate *startOfYear;
[[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitYear startDate:&startOfYear interval:0 forDate:[NSDate date]];
startOfYear now holds midnight at January 1st of the current year.
-(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;
 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With