while adding an event to the calendar of an iphone, the date is going to be reseted to 1.1.2001 and i have no idea why.
id arg1 = [args objectAtIndex:0]; // start
id arg2 = [args objectAtIndex:1]; // end
id arg3 = [args objectAtIndex:2]; // title
id arg4 = [args objectAtIndex:3]; // location
id arg5 = [args objectAtIndex:4]; // text
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *startdate = [df dateFromString: arg1];
NSDate *enddate = [df dateFromString: arg2];
EKEventStore *eventStore = [[[EKEventStore alloc] init] autorelease];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = arg3;
event.location = arg4;
event.notes = arg5;
event.startDate = startdate;
event.endDate = enddate;
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
the input seems to be fine since
NSLog([args objectAtIndex:0]);
writes the correct date. i have no real idea :(
Your input maybe correct but the formatter is not. Please, review these 2 lines. I believe the bug is there
[df setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *startdate = [df dateFromString: arg1];
[df setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
is working fine so far.
Related
Now i am working on calendar application in iPhone. I am trying to add an event to default calendar in iPhone. In this case adding an event is successfully completed.But I can't add an event in proper start time . Start date of event is exactly correct but time is different in default calendar of iPhone.I used the following code .Please help me..
NSLog(#"start label is : %#",startLabel.text);// 2013-03-28 12:03:41 PM
NSLog(#"end label is : %#",endLabel.text);//2013-03-28 01:03:41 PM
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm:ss a"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSLog(#"description is : %#",[dateFormat description]);
NSDate *sDate=[dateFormat dateFromString:startLabel.text];
NSDate *eDate = [dateFormat dateFromString:endLabel.text];
NSLog(#"startDate is : %#",sDate);//2013-03-28 12:03:41 +0000
NSLog(#"endDate is : %#",eDate);// 2013-03-28 13:03:41 +0000
self.eventStore = [[EKEventStore alloc] init];
EKEvent *myevent = [EKEvent eventWithEventStore:self.eventStore];
myevent.title = titleFld.text;
myevent.location = locFld.text;
myevent.startDate = sDate;
myevent.endDate = eDate;
[myevent setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[self.eventStore saveEvent:myevent span:EKSpanThisEvent error:&err];
use [dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
instead of
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
Hope this will solved your problem.
I Want to add my events that is in my server into ical using eventkit. Can I subscribe to a url programatically so that all the event can be sync in ical app automatically.
Synchronization is done from your side (Inside objective "c"). You need to fetching all events from server after hitting appropriate URL.
Parse it and save single event with the help of below method:
-(void)addingEvents{
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = <<Event Title>>;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd hh:mma"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSString *strDate = [dateFormatter stringFromDate:<<Your event date>>];
NSDate *startDate = [dateFormatter dateFromString:strDate];
[dateFormatter release];
event.startDate = [[NSDate alloc] initWithTimeInterval:0 sinceDate:startDate];
event.endDate = [[NSDate alloc] initWithTimeInterval:600 sinceDate:event.startDate];
NSArray *alertArray = [NSArray arrayWithObject:[EKAlarm alarmWithRelativeOffset:-300]];
event.alarms = alertArray;
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
if (err) {
NSLog(#"Error occured : %#",[err localizedDescription]);
}
[eventStore release];
}
i am trying to add the event to iPhone default calendar and the code is as follows
NSMutableString *startDateString = [NSMutableString stringWithString:#"11/20/2012 10:00 AM"];
NSMutableString *endtDateString = [NSMutableString stringWithString:#"11/20/2012 5:00 PM"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:#"MM-dd-yyyy HH:mm a"];
NSDate * startDate = [[NSDate alloc]init];
startDate = [dateFormatter dateFromString:startDateString];
NSDate *endDate = [[NSDate alloc]init];
endDate = [dateFormatter dateFromString:endtDateString];
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = #"TEST";
event.startDate = startDate;
event.endDate = endDate;
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
[startDate release];
[endDate release];
if(!err)
{
UIAlertView *alertview=[[UIAlertView alloc]initWithTitle:#"TEST" message:#"Event Added successfully " delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertview show];
[alertview release];
}
NSLog(#"error is %#",err);
}
the event is successfully adding but the problem is its not adding to event start date its just adding to jan 1st 2001 date all the events are adding to the same date only .
can any one please help me where i am getting wrong & i am using the iPhone 4s & iPhone 3gs with 5.1.1 version.
Thanks in advance .
Set DateFormat to dateFormatter like this:
[dateFormatter setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
Note: yourdateString should be according to dateFormatter
I am trying to create a new calendar in iCal. Now I am able to create a new Calendar with some name. But my problem is when I try to create to Local calendar I cant see it in calendar list. Any one have any idea about this? I have tried using all types of sources but not able to find exact solution for this.
Thank you in advance.
NSMutableArray *arrAlarm = [[NSMutableArray alloc]init];
EKAlarm *alarm1 = [EKAlarm alarmWithRelativeOffset:3600];
EKAlarm *alarm2 = [EKAlarm alarmWithRelativeOffset:-3600]; //86400 1 day
[arrAlarm addObject:alarm1];
[arrAlarm addObject:alarm2];
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *ekevent = [EKEvent eventWithEventStore:eventStore];
ekevent.title = strname;
NSLog(#"%#",ekevent.startDate);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE MMM d,yyyy"];
NSDate *date = [dateFormatter dateFromString:strdate];
// Convert to new Date Format
[dateFormatter setDateFormat:#"YYYY-MM-DD HH:MM:SS ±HHMM"];
NSString *newDate = [dateFormatter stringFromDate:date];
// dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
// [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease]];
[dateFormatter setDateFormat:#"hh:mm a"];
NSDate* firstDate = [dateFormatter dateFromString:time];
NSDate* secondDate = [dateFormatter dateFromString:endtime];
NSTimeInterval timeDifference = [secondDate timeIntervalSinceDate:firstDate];
// Convert to new Date Format
[dateFormatter setDateFormat:#"HH:MM"];
NSString *t11 = [dateFormatter stringFromDate:firstDate];
NSString *t12 = [dateFormatter stringFromDate:secondDate];
NSDate *dt1 = [dateFormatter dateFromString:t11];
NSDate *dt2 = [dateFormatter dateFromString:t12];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"hh"];
NSString *hourstring = [NSString stringWithFormat:#"%#",
[df stringFromDate:dt1]];
[df setDateFormat:#"mm"];
NSString *minstring = [NSString stringWithFormat:#"%#",
[df stringFromDate:dt1]];
[df release];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *components = [gregorian components: NSUIntegerMax fromDate: date];
[components setHour: [hourstring intValue]];
[components setMinute: [minstring intValue]];
[components setSecond: 00];
// NSDate *newDate = [gregorian dateFromComponents: components];
[gregorian release];
ekevent.startDate = date;
ekevent.endDate = [[NSDate alloc] initWithTimeInterval:timeDifference sinceDate:date];
[ekevent setCalendar:[eventStore defaultCalendarForNewEvents]];
ekevent.alarms = arrAlarm;
[arrAlarm release];
NSError *err;
[eventStore saveEvent:ekevent span:EKSpanThisEvent error:&err];
Try this code you will get the desired result.
I am able to fetch NSString representing the current date and time in PST using the following code.
//get current date and time
NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:#"America/Los_Angeles"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yy HH:mm:ss zzz"];
[dateFormatter setTimeZone:timeZone];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
[enUSPOSIXLocale release];
NSDate *date = [NSDate date];
NSString *currentDateStr = [dateFormatter stringFromDate:date];
NSLog(#"currentDateStr %#",currentDateStr);
[dateFormatter release];
EDIT:
But the following code to fetch NSDate from NSString doesn't serve my purpose.
//get date from string
NSDateFormatter* formatter2 = [[NSDateFormatter alloc] init];
[formatter2 setDateFormat:#"MM/dd/yy HH:mm:ss"];
NSDate *dt = [[NSDate alloc]init];
dt = [formatter2 dateFromString:currentDateStr];
NSLog(#"dt %#",dt);
"dt" appears to be null.
Could anybody help me out in solving this issue of getting NSDate from NSString.
Thanx in advance.
NSDateFormatter also has dateFromString: function.
EDIT:
//get date from string.
NSDateFormatter* formatter2 = [[NSDateFormatter alloc] init];
[formatter2 setDateFormat:#"MM/dd/yy HH:mm:ss zzz"];
NSDate *dt = [formatter2 dateFromString:currentDateStr];
NSLog(#"dt %#",dt);
Make use of
[dateFormatter dateFromString:urString];
In your case urString is currentDateStr
Use
- (NSString *)datePickerValueChangedd:(UIDatePicker*) datePicker{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 50, 68, 68)];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
label.text = [NSString stringWithFormat:#"%#",[df stringFromDate:datePicker.date]];
NSLog(#"I am inside the datePickerValueChanged - - %#", label.text);
[df release];
globalVariable= label.text;
return (label.text);
}