我正在尋找可以處理特定日期的組件。objective c和NSDate
我想要做的是讓我的iPhone中的代表日期(例如04-08-2012),我希望能夠「工作」這個日期的Astring前線服務器。這樣比較它到另一個日期,檢查過去或未來的日期,並將其打印到應用程序GUI
我試過與NSDate工作,但我沒有找到如何設置特定日期?
感謝
我正在尋找可以處理特定日期的組件。objective c和NSDate
我想要做的是讓我的iPhone中的代表日期(例如04-08-2012),我希望能夠「工作」這個日期的Astring前線服務器。這樣比較它到另一個日期,檢查過去或未來的日期,並將其打印到應用程序GUI
我試過與NSDate工作,但我沒有找到如何設置特定日期?
感謝
您可以使用NSDateFormatter
這裏是一個示例代碼從字符串分析日期:
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"%A, %B %d, %Y"];
NSDate* date = [formatter dateFromString:aString];
[date compare:anotherDate];
More about Date Formatter here
日期格式字符串由拉出日期部分的各種元素組成。 %A是星期幾的全名,%B是月份的全名,等等。
您需要3類設置由代表日期:
NSCalendar* cal = [NSCalendar currentCalendar]; // 1.
NSDateComponents *comps = [[NSDateComponents alloc] init]; // 2.
[comps setYear:2012];
[comps setMonth:4];
[comps setDay:8];
NSDate* date = [cal dateFromComponents:comps]; // 3.
[comps release];