开发者

How can I change NSDateFormatter to "DD/MM/YYYY"?

开发者 https://www.devze.com 2023-04-05 12:55 出处:网络
I have tried putting this: EKEventStore *eventStore = [[EKEventStore alloc] init]; EKEvent *event= [EKEvent eventWithEventStore:eventStore];

I have tried putting this:

 EKEventStore *eventStore = [[EKEventStore alloc] init];

 EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
 event.title = [NSString stringWithFormat:@"DHSB Assignment: %@", Assignment1.text];

 NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
 [formatter setDateFormat:@"M/d"];
 event.startDate = [formatter dateFromString:dateField.text];

 NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
 [formatter2 setDateFormat:@"M/d"];
 event.endDate = [formatter2 dateFromString:dateField.text];

 [event setCalendar:[eventStore defaultCalendarForNewEvents]];
 [eventStore saveEvent:event span:EKSpanThisEvent error:&err]; 
 [EKEventStore release];

 NSLog(@"Successfully added '%@' to the calendar", Assignment1.text);

...but the event isn't added t开发者_开发知识库o the calendar if I enter a date in the format of "DD/MM/YYYY".

Why is this?

Thanks!


I think the correct format should be:

[formatter setDateFormat:@"dd/MM/yyyy"];

Try putting a NSLog on the dateFromString method to be sure you're getting the right result

EDIT: you may also need to set the correct locale for the date according to your needs.


I think your format string is wrong:

@"dd/MM/yyyy";

Might give better results.

By the way - what is the point of passing in an NSError* pointer to a function and not checking the return value or the error. Try replacing this line:

[eventStore saveEvent:event span:EKSpanThisEvent error:&err]; 

With something like this:

BOOL success = [eventStore saveEvent:event span:EKSpanThisEvent error:&err];

if (!success) {
    NSLog(@"Cannot save event. %@", [err localizedDescription];
}

Not perfect error checking but it will give you some idea of what is happening while you are developing.

0

精彩评论

暂无评论...
验证码 换一张
取 消