Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EKEvent incorrect date when allday is set

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?

like image 837
kos Avatar asked Dec 20 '22 15:12

kos


1 Answers

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.

like image 114
pjh68 Avatar answered May 10 '23 16:05

pjh68