日期格式

时间:2013-03-21 10:08:31

标签: iphone ipad ios6 calendar

使用以下格式在日历中添加活动时,它会在Calendar Alert中添加为:

  "1 day before",

但我想要

  "At the time of event"

这是我的代码,

calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSString *cday,*cmonth,*cyear;
// set today
today = [[NSDate alloc] initWithTimeIntervalSinceNow:1];
//set the current day to show the calendar
NSDateFormatter *temp1 = [[NSDateFormatter alloc] init];
[temp1 setDateFormat:@"dd"];
cday=[temp1 stringFromDate:today];
NSLog(@"Date checking%@",cday);
[temp1 release];

我的代码有什么问题?任何人都可以帮我解决一下。

1 个答案:

答案 0 :(得分:0)

可能与时区有关。您可能需要为NSDateFormatter对象的timeZone属性设置适当的值。请将时区设置为本地时区

[temp1 setTimeZone:[NSTimeZone systemTimeZone]];

(or)

[temp1 setTimeZone:[NSTimeZone localTimeZone]];

请输入此代码:

 NSCalendar  *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSString *cday,*cmonth,*cyear;
    // set today
    NSDate *today = [NSDate date];
    //set the current day to show the calendar
    NSDateFormatter *temp1 = [[NSDateFormatter alloc] init];
   [temp1 setTimeZone:[NSTimeZone localTimeZone]];
    [temp1 setDateFormat:@"dd"];
    cday=[temp1 stringFromDate:today];
    NSLog(@"Date checking%@",cday);
    [temp1 release];