简体   繁体   English

设置iPhone日历事件的特定时间

[英]Set specific time for iphone calendar event

Hello I am trying to add calendar events to iphone's calendar, but am unsure how to set a specific date and time. 您好,我正在尝试将日历事件添加到iPhone的日历中,但是不确定如何设置特定的日期和时间。 I have looked over the answer from here Programmatically add custom event in the iPhone Calendar 我从这里查看了答案,以编程方式在iPhone日历中添加自定义事件

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. 任何帮助将不胜感激,谢谢。

Take a look here . 在这里看看。

There's couple examples as well how to use NSDateComponents . 还有几个示例,以及如何使用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];

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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM