-2

Consider this code:

  NSDateComponents *comps = [[NSDateComponents alloc] init];
  [comps setDay:2];
  [comps setMonth:6];
  [comps setYear:2016];

  NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

  NSDate *date = [gregorian dateFromComponents:comps];

If I print the date to console it is

2016-06-01 23:00:00 +0000

June 1, instead of June 2.

Is there a way to create the dates I want without iOS converting that to some crazy date?

Duck
  • 34,902
  • 47
  • 248
  • 470

2 Answers2

1

In my time zone, it is GMT+0800 (GMT+08:00) offset 28800, so print the date to console it is

2016-06-01 16:00:00 +0000

if you want date print 2016-06-02 00:00 +0000, you need set timezone gmt to 0.

gregorian.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
Duck
  • 34,902
  • 47
  • 248
  • 470
Reming Hsu
  • 2,215
  • 15
  • 18
  • thanks that solves the problem but is insane how it works, or not. – Duck Jul 01 '15 at 08:11
  • @SpaceDog Out of interest, what are you using the `NSDate` object for? – Droppy Jul 01 '15 at 08:12
  • it is a kind of planner that just needs to be aware of the day/month of a given date, not the year. So, when I set a date I need that date to be the date I set, not the date iOS insanely decides. – Duck Jul 01 '15 at 08:20
  • Well if you ever wish to use the `NSDate` object for notifications or timers then this answer will not solve your issue as the notifications will go off at the wrong time, WRT the user's current location. You simply need to understand that `NSDate` does not carry timezone information, so that needs to be specified to the objects that manipulate `NSDate` objects. – Droppy Jul 01 '15 at 08:24
  • @SpaceDog No, it is not insane: http://stackoverflow.com/questions/17294289/unexpected-value-from-nsdate/17296378#17296378 – Amin Negm-Awad Jul 01 '15 at 08:42
1

In a word "timezone".

It's the 1st June somewhere in the world and 2nd June in other places in the world, all at the same instant.

Configure your timezones and understand that [NSDate description] displays date/times in UTC and that [NSDate description] is used by both NSLog() and the debugger and you should be most of the way there.

Droppy
  • 9,691
  • 1
  • 20
  • 27
  • this doesn't make sense. Suppose I want an alarm to fire June 2, 2016. When my iPhone clock shows that date, it will fire. Why do I have to care if is June 2 already in other places if I am not there. When June 2 comes for me, then it is June 2. This is stupid. – Duck Jul 01 '15 at 08:03
  • 1
    @SpaceDog You don't have to care; it's just when you want to display the date/time in the debugger or log that you get confused as that displays the date/time in UTC. Therefore in order to display `NSDate` objects you need to use a correctly configured `NSDateFormatter` object. – Droppy Jul 01 '15 at 08:04
  • No it is not. After creating the June 2 date I calculate the number of days between that and another date and store that value on a variable. Then I count manually on a calendar and the number on the variable is always one short. – Duck Jul 01 '15 at 08:06
  • @SpaceDog Sorry I don't see that detail in your question. – Droppy Jul 01 '15 at 08:06