4

I have this code:

NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setYear:2011];
[components setDay:1];
[components setMonth:4];
NSCalendar *gregorianCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *firstDate = [gregorianCalendar dateFromComponents:components];
NSLog(@"date:%@", firstDate);

the result in console is:

date:2011-03-31

why???? if I set "setDay:1" it would be "date:2011-04-01", no?

kubi
  • 48,104
  • 19
  • 94
  • 118
cyclingIsBetter
  • 17,447
  • 50
  • 156
  • 241
  • 2
    That's not the full result of the `NSLog` call. It should look something like this: `2011-05-04 17:45:48.698 DatePrint[55569:a0b] date:2011-04-01 00:00:00 -0000`, i.e., the date will print with the time and time zone. I get the expected result when running your code. Please post the complete output of your log. – jscs May 04 '11 at 17:50
  • 2011-05-05 09:23:29.268 Project1[92:707] date:2011-04-30 22:00:00 +0000.....I set year 2011, month 5 and day 1. – cyclingIsBetter May 05 '11 at 07:25
  • Looks like it could be a time zone/"summer time" discrepancy. – jscs May 05 '11 at 17:00

3 Answers3

2

I think this could be a time zone related problem. Be sure to set your time zone on NSDateComponents:

[ components setTimeZone:[ NSTimeZone systemTimeZone ] ] ;
nielsbot
  • 15,922
  • 4
  • 48
  • 73
  • I solve with [components setTimeZone:[ NSTimeZone timeZoneForSecondsFromGMT:(+0*3600) ] ] ; – cyclingIsBetter May 05 '11 at 17:35
  • 2
    I think this may cause you trouble later. When you create a date from 2011-1-4, that's midnight in your current time zone. When the date is printed however, they print your date at GMT. That's why there's a discrepancy. Unless your date/time calculations are wrong, you might stick with `[ NSTimeZone systemTimeZone ]` (or none at all) – nielsbot May 05 '11 at 17:42
  • Doesn't work. I have applied `setTimeZone` to both `calendar` and `components` objects. – Martin Berger Aug 22 '13 at 10:11
1

Also it depends on the date formatting. My dates were 1 year old for YYYY and I change it to yyyy (lower case) then it was fixed. Be careful about that.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
0

Try:

   [components setWeekdayOrdinal:1]; // The first day in the month

From the NSDateComponents docs:

You can configure an instance of NSDateComponents to specify the components of a date and then use the NSCalendar method dateFromComponents: to create the corresponding date object. You can provide as many components as you need (or choose to). When there is incomplete information to compute an absolute time, default values such as 0 and 1 are usually chosen by a calendar, but this is a calendar-specific choice. If you provide inconsistent information, calendar-specific disambiguation is performed (which may involve ignoring one or more of the parameters).

Rayfleck
  • 12,116
  • 8
  • 48
  • 74
  • 2 things: try some different numbers (other than day==1), like 15, so maybe you can determine what's going on, and also post your entire NSLog line as @Josh Caswell pointed out. – Rayfleck May 05 '11 at 12:26