0

Hello I am trying to add calendar events to iphone's calendar, but am unsure how to set a specific date and time. I have looked over the answer from here Programmatically add custom event in the iPhone Calendar

so I currently have:

Store.startDate = [NSDate Date] //this part is what I would like to set a specific year/month/day/hour/minutes at.

Any help would be greatly appreciated, Thanks.

Community
  • 1
  • 1

2 Answers2

0

Take a look here.

There's couple examples as well how to use NSDateComponents.

NSDateComponents *comps = [[NSDateComponents alloc] init];

// date
[comps setDay:10];
[comps setMonth:8];
[comps setYear:2015];

// time
[comps setHour:17];
[comps setMinute:13];
[comps setSecond:21];

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:comps];
Juri Noga
  • 4,363
  • 7
  • 38
  • 51
0

Get Current Date

 NSLocale *en_US_POSIX = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
    assert(en_US_POSIX != nil);
    NSDate *currentTime = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setLocale:en_US_POSIX];
    [dateFormatter setDateFormat:@"dd-MM-yyyy"];
    NSString *resultString = [dateFormatter stringFromDate: currentTime];

Get Current Time

NSDate* now = [NSDate date];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *dateComponents = [gregorian components:(NSCalendarUnitHour  | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:now];
    NSInteger hour = [dateComponents hour];
    NSString *am_OR_pm=@"AM";
    if (hour>12){
        hour=hour%12;
        am_OR_pm = @"PM";
    }
    NSInteger minute = [dateComponents minute];
    NSString *strTime=[NSString stringWithFormat:@"%02d:%02d %@", hour, minute ,am_OR_pm];
Mehul
  • 3,033
  • 23
  • 37