4

If today is Tuesday 2PM May 6th

event.startDate = [[NSDate alloc] init];
event.allDay = YES;
[dateFormat setDateFormat:EEEE, MMMM dd, yyyy];
NSString* dayStr = [dateFormat stringFromDate:event.startDate];
timeDetails.text = [NSString stringWithFormat:@"%@\nAll day", dayStr];

results in Monday May 5th

same code without allDay set

event.startDate = [[NSDate alloc] init];
[dateFormat setDateFormat:EEEE, MMMM dd, yyyy];
NSString* dayStr = [dateFormat stringFromDate:event.startDate];
timeDetails.text = [NSString stringWithFormat:@"%@\nAll day", dayStr];

results in Tuesday May 6th which is correct. Anyone has any clues?

kos
  • 1,357
  • 9
  • 21

2 Answers2

11

Background

In iOS all dates have a time component. So for an all day event, some time is still used. Typically this is midnight to "just before" midnight (23:59), giving the event a span of almost 24 hours.

The challenge comes with timezones and daylight saving. This can mean thats all day events start at 11pm the previous day and finish at 22:59. It all depends on where you are viewing the event from. Unfortunately in iOS development this often isn't clear! I've had some really "fun" bug that only materialise when running the app between 11pm and midnight before.

Fix

Amazingly the behaviour of EKEvent changes according the order that you set the properties. If you set event.allDay = YES before you set the startDate then you will get the behaviour you are expecting.

pjh68
  • 443
  • 4
  • 6
1

It may be related to timezone. Cannot confirm it though, since even if I try setting formatter timezone it still produces the same problem. More importantly I would have assumed that if I create NSData and use NSFormatter on it by default it would all match.

kos
  • 1,357
  • 9
  • 21