您好我正在尝试将日历事件添加到iphone的日历中,但我不确定如何设置特定的日期和时间。我在这里查看了答案Programmatically add custom event in the iPhone Calendar
所以我现在有:
Store.startDate = [NSDate Date] //this part is what I would like to set a specific year/month/day/hour/minutes at.
非常感谢任何帮助,谢谢。
答案 0 :(得分:0)
看看here。
还有几个示例以及如何使用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];
答案 1 :(得分:0)
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];
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];