Search code examples
iosobjective-ccocoa-touchnscalendar

How to get actual first day of the month, possible with my time zone?


I'm trying to get the first day of the month, but I'm getting the UTC value. I think I need my locale date instead?

enter image description here


Solution

  • I've tried this myself:

    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *dateComponents = [gregorian components:( NSYearCalendarUnit |
                                                              NSMonthCalendarUnit | NSDayCalendarUnit ) fromDate:[NSDate date]];
    [dateComponents setDay:1];
    NSLog(@"%@", [gregorian dateFromComponents:dateComponents]);
    

    And it properly prints 2015-04-30 22:00:00 +0000 which would be 1st of May after adding 2 hour (in my case) timezone difference. Just re-check your code, seems like self.calender.currentDate gives wrong month?