0

I am trying to save reminder in calendar. Its been saved but with wrong date and time.

EDIT Basically i want to save reminder in calendar with start date selected from startDate picker and end date selected from endDate picker. and time selected from reminderTime picker. Any date and time.

Below is my code. What i am doing wrong here

EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
event.title = productTextField.text;

   [event setCalendar:[eventStore defaultCalendarForNewEvents]];

NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];//This is working

NSDateComponents *components = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[reminderTime getDate]];

NSInteger hour = [components hour];
NSInteger minute = [components minute];
NSDate *reminderDateAndTime = [NSDate dateWithTimeIntervalSince1970: [[startDate getDate] timeIntervalSince1970] + (minute * 60) + (hour * 60 * 60)];
 NSDate *endDateAndTime = [NSDate dateWithTimeIntervalSince1970: [[endDate getDate] timeIntervalSince1970] ];
 event.startDate = reminderDateAndTime;

event.endDate   = endDateAndTime;

    [eventStore saveEvent:event span:EKSpanThisEvent error:&err];
Mann
  • 5,477
  • 6
  • 45
  • 57

2 Answers2

4

Basically i want to save reminder in calendar with start date selected from startDate picker and end date selected from endDate picker. and time selected from reminderTime picker. Any date and time.

You don't need 2 or 3 pickers as well, you can use only one picker for all this operation.

Seems like allocation is taking much when you use 3 pickers, one for start date, one for end date and other for time and that UI looks funky as well.

Then why are you using dateWithTimeIntervalSince1970: . It returns an NSDate object set to the given number of seconds from the first instant of 1 January 1970, GMT. You don't need it.

Just take a BOOL for example:BOOL dateValue

if dateValue is true, get your start Date and if it is false get your end date.

Just get the first selected date like:

NSDate *startDate = [datePicker date];

Note: After selecting the start Date, set the minimum Date of your picker as your first selected start date.

Then user can't select the end Date before the start Date.

Charan
  • 4,940
  • 3
  • 26
  • 43
  • Thanks for answer but i want to add time also. And end date also of reminder – Mann Oct 30 '12 at 11:51
  • Then format the date along with the time.Your date picker also provides time along with date, day and year. you can check this -> http://www.unicode.org/reports/tr35/#Date_Format_Patterns – Charan Oct 30 '12 at 11:52
  • But how it would respond to different locations? – Mann Oct 30 '12 at 12:13
  • what do you mean by different locations? – Charan Oct 30 '12 at 12:25
  • If app user is in Soudi arab instead of England then? – Mann Oct 30 '12 at 12:27
  • That doesn't make any sense though, Date picker get the date according to time set in the device, date picker doesn't know where the iPhone user is using the iPhone. – Charan Oct 30 '12 at 12:35
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/18789/discussion-between-sree-charan-and-mann) – Charan Oct 30 '12 at 12:37
0

I figured out what i was doing wrong. Below code works fine (posting code here for other users)

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];


// Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit )
                                               fromDate:[startDate getDate]];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
                                               fromDate:[reminderTime getDate]];

NSDateComponents *dateComponentsEnd = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit )
                                               fromDate:[endDate getDate]];

 [event setCalendar:[eventStore defaultCalendarForNewEvents]];

// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
// Notification will fire in one minute
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]];
NSDate *startDatee = [calendar dateFromComponents:dateComps];
NSDateComponents *dateCompsEnd = [[NSDateComponents alloc] init];
[dateCompsEnd setDay:[dateComponentsEnd day]];
[dateCompsEnd setMonth:[dateComponentsEnd month]];
[dateCompsEnd setYear:[dateComponentsEnd year]];
NSDate *endDatee = [calendar dateFromComponents:dateCompsEnd];
event.startDate=startDatee;
event.endDate=endDatee;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
Mann
  • 5,477
  • 6
  • 45
  • 57
  • Hi mann,I have a question. What is your startDate, reminderTime, and endDate? And the event.startDate and event.endDate is a relative date? not an absolute date? Thanks! – EVA Nov 07 '12 at 09:39
  • startDate, endDate adn reminderTime are pickers. and about event.startDate and event.endDate being relative or absolute dont get by your saying im afraid. It will save date in unixtimestamp – Mann Nov 07 '12 at 09:43
  • Thanks mann. My problem is when I save a reminder, it said "Invalid date components." Do you have some any clues? Thanks a again. – EVA Nov 08 '12 at 02:10
  • Dont know mate. I wish i could help you here. But really dont know – Mann Nov 08 '12 at 09:46