8

I have this subset of a method that needs to get day one of the current month.

NSDate *today = [NSDate date];  // returns correctly 28 february 2013
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components.day = 1;
NSDate *dayOneInCurrentMonth = [gregorian dateByAddingComponents:components toDate:today options:0];

dayOneInCurrentMonth then prints out 2013-03-01 09:53:49 +0000, the first day of the next month.

How do I get day one of the current month?

jscs
  • 63,694
  • 13
  • 151
  • 195
bogen
  • 9,954
  • 9
  • 50
  • 89

6 Answers6

17

Your logic is wrong: Instead of setting the date's day to 1, you're adding a day to the current date.

Try something like that:

NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *components = [gregorian components:(NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit) fromDate:today];
components.day = 1;

NSDate *dayOneInCurrentMonth = [gregorian dateFromComponents:components];
Fabian Kreiser
  • 8,307
  • 1
  • 34
  • 60
  • Of course! Misread the method name. Thank you! Will accept answer :-) – bogen Feb 28 '13 at 10:08
  • 2
    Ok, @Fabian Kreiser, I used the code as is and it gives me result: `2015-05-31 21:00:00 UTC`. Today is `2015-06-08 8:36`. It does not returns 1st day, but last from previous month? – new2ios Jun 08 '15 at 07:37
1

You want [NSCalender dateFromComponents:] instead:

NSDate *dayOneInCurrentMonth = [gregorian dateFromComponents:components];
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
1

Easy way of getting to the 1st from a given date:

NSDate *first = [gregorian dateBySettingUnit:NSCalendarUnitDay value:1 ofDate:date options:0]; 
Graham Perks
  • 23,007
  • 8
  • 61
  • 83
1

Easy Way to get first and last date of previous month is :

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *comp = [gregorian components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay ) fromDate:[NSDate date]];

//TO GET PREVIOUS MONTH LAST DAY
[comp setMonth:[comp month]];
[comp setDay:1];
NSDate *tDateMonth = [gregorian dateFromComponents:comp];
NSLog(@"LAST DAY OF PREVIOUS MONTH ;  %@", tDateMonth);

//TO GET PREVIOUS MONTH FIRST DAY
[comp setMonth:[comp month]-1];
[comp setDay:3];
NSDate *td = [gregorian dateFromComponents:comp];
NSLog(@"FIRST DAY OF PREVIOUS MONTH ;  %@", td);

Hope this helps

ck1924
  • 9
  • 3
0

By creating new variable is expensive on memory usage, it's better using date formatter to get the first day of the current month

today = [NSDate date];

firstDayDateFormatter = [[NSDateFormatter alloc] init];
[firstDayDateFormatter setDateFormat:@"01-MM-yyyy"];
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US"]];
firstDayOfTheMonth = [mdfDateFormat dateFromString:[firstDayDateFormatter stringFromDate:today]];
Neo
  • 167
  • 1
  • 1
  • 12
0
NSDate *startDate = nil;
[[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitMonth startDate:&startDate interval:NULL forDate:date];

return startDate;
leonardosccd
  • 1,503
  • 11
  • 12